Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(presets): add 3 new presets to work with ESM (#2207)
BREAKING CHANGE
- `create-jest-preset` util function signature has changed to
```
function createJestPreset(allowJs?: boolean, extraOptions?: Config.InitialOptions): TsJestPresets;
```
  • Loading branch information
ahnpnl committed Dec 12, 2020
1 parent 753b5e4 commit c277858
Show file tree
Hide file tree
Showing 17 changed files with 224 additions and 162 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -14,7 +14,7 @@

<img src="./icon.png" align="right" title="ts-jest Logo" width="128" height="128">

It supports all features of TypeScript including type-checking. [Read more about Babel7 + `preset-typescript` **vs** TypeScript (and `ts-jest`)](https://kulshekhar.github.io/ts-jest/user/babel7-or-ts).
It supports all features of TypeScript including type-checking. [Read more about Babel7 + `preset-typescript` **vs** TypeScript (and `ts-jest`)](https://kulshekhar.github.io/ts-jest/docs/babel7-or-ts).

---

Expand Down
15 changes: 9 additions & 6 deletions docs/docs/presets.md
Expand Up @@ -3,15 +3,18 @@ id: presets
title: Presets
---

### The 3 presets
### The presets

`ts-jest` comes with 3 presets, covering most of the project's base configuration:
`ts-jest` comes with several presets, covering most of the project's base configuration:

| Preset name | Description |
|---|---|
| `ts-jest/presets/default`<br/>or `ts-jest` | `ts-jest` will take care of `.ts` and `.tsx` files only, leaving JavaScript files as-is. |
| `ts-jest/presets/js-with-ts` | TypeScript and JavaScript files (`.ts`, `.tsx`, `.js` and `.jsx`) will be handled by `ts-jest`.<br/>You'll need to set `allowJs` to `true` in your `tsconfig.json` file. |
| `ts-jest/presets/js-with-babel` | TypeScript files will be handled by `ts-jest`, and JavaScript files will be handled by `babel-jest`. |
| `ts-jest/presets/default`<br/>or `ts-jest` | TypeScript files (`.ts`, `.tsx`) will be transformed by `ts-jest` to **CommonJS** syntax, leaving JavaScript files (`.js`, `jsx`) as-is. |
| `ts-jest/presets/default-esm`<br/> | TypeScript files (`.ts`, `.tsx`) will be transformed by `ts-jest` to **ESM** syntax, leaving JavaScript files (`.js`, `jsx`) as-is. |
| `ts-jest/presets/js-with-ts` | TypeScript and JavaScript files (`.ts`, `.tsx`, `.js`, `.jsx`) will be transformed by `ts-jest` to **CommonJS** syntax.<br/>You'll need to set `allowJs` to `true` in your `tsconfig.json` file. |
| `ts-jest/presets/js-with-ts-esm` | TypeScript and JavaScript files (`.ts`, `.tsx`, `.js`, `.jsx`, `.mjs`) will be transformed by `ts-jest` to **ESM** syntax.<br/>You'll need to set `allowJs` to `true` in your `tsconfig.json` file. |
| `ts-jest/presets/js-with-babel` | TypeScript files (`.ts`, `.tsx`) will be transformed by `ts-jest` to **CommonJS** syntax, and JavaScript files (`.js`, `jsx`) will be transformed by `babel-jest`. |
| `ts-jest/presets/js-with-babel-esm` | TypeScript files (`.ts`, `.tsx`) will be transformed by `ts-jest` to **ESM** syntax, and JavaScript files (`.js`, `jsx`, `.mjs`) will be transformed by `babel-jest`. |

### Basic usage

Expand All @@ -27,7 +30,7 @@ module.exports = {
};
```

```js
```json5
// OR package.json
{
// [...]
Expand Down
40 changes: 36 additions & 4 deletions e2e/__cases__/presets/ts-jest-presets.spec.ts
@@ -1,9 +1,41 @@
// preset and utils should work all the time
import * as presets from 'ts-jest/presets'
import { JS_EXT_TO_TREAT_AS_ESM, TS_EXT_TO_TREAT_AS_ESM } from 'ts-jest/dist/constants'

test('presets', () => {
const presetKeys = ['transform']
expect(Object.keys(presets.defaults)).toEqual(presetKeys)
expect(Object.keys(presets.jsWithBabel)).toEqual(presetKeys)
expect(Object.keys(presets.jsWithTs)).toEqual(presetKeys)
expect(presets.defaults).toEqual({
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
})
expect(presets.defaultsESM).toEqual({
extensionsToTreatAsEsm: [...TS_EXT_TO_TREAT_AS_ESM],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
})
expect(presets.jsWithTs).toEqual({
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
})
expect(presets.jsWithTsESM).toEqual({
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
transform: {
'^.+\\.m?[tj]sx?$': 'ts-jest',
},
})
expect(presets.jsWithBabel).toEqual({
transform: {
'^.+\\.tsx?$': 'ts-jest',
'^.+\\.jsx?$': 'babel-jest',
},
})
expect(presets.jsWithBabelESM).toEqual({
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
transform: {
'^.+\\.tsx?$': 'ts-jest',
'^.+\\.m?[j]sx?$': 'babel-jest',
},
})
})
8 changes: 0 additions & 8 deletions e2e/__cases__/ts-jest-checks/index.spec.ts

This file was deleted.

35 changes: 0 additions & 35 deletions e2e/__tests__/__snapshots__/jest-presets.test.ts.snap

This file was deleted.

28 changes: 2 additions & 26 deletions e2e/__tests__/jest-presets.test.ts
@@ -1,34 +1,10 @@
import { allValidPackageSets, PackageSets } from '../__helpers__/templates'
import { PackageSets } from '../__helpers__/templates'
import { configureTestCase } from '../__helpers__/test-case'

// 'ts-jest' is tested in almost all test cases
// 'ts-jest/presets/default' is an alias of the above
// 'ts-jest/presets/js-with-ts' is tested in allow-js.test.ts

describe('ts-jest/presets/js-with-babel', () => {
const testCase = configureTestCase('preset-with-babel', { jestConfig: { preset: 'ts-jest/presets/js-with-babel' } })

testCase.runWithTemplates([PackageSets.default], 1, (runTest, { testLabel }) => {
it(testLabel, () => {
const result = runTest()
expect(result.status).toBe(1)
expect(result.stderr).toMatch(/(Couldn't|Cannot) find (preset|module) ["']@babel\/preset-env["']/)
})
})

testCase.runWithTemplates([PackageSets.babel7, PackageSets.babel7StringConfig], 0, (runTest, { testLabel }) => {
it(testLabel, () => {
const result = runTest()
expect(result.status).toBe(0)
expect(result).toMatchSnapshot()
})
})
})

describe('ts-jest all presets', () => {
const testCase = configureTestCase('presets')

testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => {
testCase.runWithTemplates([PackageSets.default], 0, (runTest, { testLabel }) => {
it(testLabel, () => {
const result = runTest()
expect(result.status).toBe(0)
Expand Down
13 changes: 0 additions & 13 deletions e2e/__tests__/ts-jest-checks.test.ts

This file was deleted.

1 change: 1 addition & 0 deletions presets/default-esm/jest-preset.js
@@ -0,0 +1 @@
module.exports = require('..').defaultsESM
30 changes: 21 additions & 9 deletions presets/index.js
@@ -1,20 +1,32 @@
const { JS_EXT_TO_TREAT_AS_ESM, TS_EXT_TO_TREAT_AS_ESM } = require('../dist/constants')
const { createJestPreset } = require('../dist/presets/create-jest-preset')

module.exports = {
get defaults() {
return createJestPreset()
},
get defaultsESM() {
return createJestPreset(false, { extensionsToTreatAsEsm: TS_EXT_TO_TREAT_AS_ESM })
},
get jsWithTs() {
return createJestPreset({ allowJs: true })
return createJestPreset(true)
},
get jsWithTsESM() {
return createJestPreset(true, { extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM] })
},
get jsWithBabel() {
return createJestPreset(
{ allowJs: false },
{
transform: {
'^.+\\.jsx?$': 'babel-jest',
},
}
)
return createJestPreset(false, {
transform: {
'^.+\\.jsx?$': 'babel-jest',
},
})
},
get jsWithBabelESM() {
return createJestPreset(false, {
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
transform: {
'^.+\\.m?[j]sx?$': 'babel-jest',
},
})
},
}
1 change: 1 addition & 0 deletions presets/js-with-babel-esm/jest-preset.js
@@ -0,0 +1 @@
module.exports = require('..').jsWithBabelESM
1 change: 1 addition & 0 deletions presets/js-with-ts-esm/jest-preset.js
@@ -0,0 +1 @@
module.exports = require('..').jsWithTsESM
3 changes: 3 additions & 0 deletions src/constants.ts
Expand Up @@ -2,6 +2,9 @@ export const LINE_FEED = '\n'
export const TS_TSX_REGEX = /\.tsx?$/
export const JS_JSX_REGEX = /\.jsx?$/
export const DECLARATION_TYPE_EXT = '.d.ts'
// `extensionsToTreatAsEsm` only accepts `.ts`, `.tsx` and `.jsx`. `.js` and `.mjs` will throw error
export const TS_EXT_TO_TREAT_AS_ESM = ['.ts', '.tsx']
export const JS_EXT_TO_TREAT_AS_ESM = ['.jsx']
/**
* @internal
* See https://jestjs.io/docs/en/configuration#testmatch-arraystring
Expand Down
76 changes: 76 additions & 0 deletions src/presets/__snapshots__/create-jest-preset.spec.ts.snap
@@ -0,0 +1,76 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`create-jest-preset should return correct preset 1`] = `
Object {
"transform": Object {
"^.+\\\\.tsx?$": "ts-jest",
},
}
`;

exports[`create-jest-preset should return correct preset 2`] = `
Object {
"transform": Object {
"^.+\\\\.tsx?$": "ts-jest",
},
}
`;

exports[`create-jest-preset should return correct preset 3`] = `
Object {
"transform": Object {
"^.+\\\\.[tj]sx?$": "ts-jest",
},
}
`;

exports[`create-jest-preset should return correct preset 4`] = `
Object {
"transform": Object {
"^.+\\\\.[tj]sx?$": "ts-jest",
},
}
`;

exports[`create-jest-preset should return correct preset 5`] = `
Object {
"transform": Object {
"^.+\\\\.tsx?$": "ts-jest",
},
}
`;

exports[`create-jest-preset should return correct preset 6`] = `
Object {
"moduleFileExtensions": Array [
"bar",
],
"testMatch": Array [
"foo",
],
"transform": Object {
"^.+\\\\.tsx?$": "ts-jest",
"foo": "bar",
},
}
`;

exports[`create-jest-preset should return correct preset 7`] = `
Object {
"extensionsToTreatAsEsm": Array [
".jsx",
".ts",
".tsx",
],
"moduleFileExtensions": Array [
"bar",
],
"testMatch": Array [
"foo",
],
"transform": Object {
"^.+\\\\.m?[tj]sx?$": "ts-jest",
"foo": "bar",
},
}
`;

0 comments on commit c277858

Please sign in to comment.