Skip to content

Commit c277858

Browse files
authored
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; ```
1 parent 753b5e4 commit c277858

17 files changed

Lines changed: 224 additions & 162 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

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

17-
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).
17+
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).
1818

1919
---
2020

docs/docs/presets.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ id: presets
33
title: Presets
44
---
55

6-
### The 3 presets
6+
### The presets
77

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

1010
| Preset name | Description |
1111
|---|---|
12-
| `ts-jest/presets/default`<br/>or `ts-jest` | `ts-jest` will take care of `.ts` and `.tsx` files only, leaving JavaScript files as-is. |
13-
| `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. |
14-
| `ts-jest/presets/js-with-babel` | TypeScript files will be handled by `ts-jest`, and JavaScript files will be handled by `babel-jest`. |
12+
| `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. |
13+
| `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. |
14+
| `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. |
15+
| `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. |
16+
| `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`. |
17+
| `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`. |
1518

1619
### Basic usage
1720

@@ -27,7 +30,7 @@ module.exports = {
2730
};
2831
```
2932

30-
```js
33+
```json5
3134
// OR package.json
3235
{
3336
// [...]
Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
// preset and utils should work all the time
22
import * as presets from 'ts-jest/presets'
3+
import { JS_EXT_TO_TREAT_AS_ESM, TS_EXT_TO_TREAT_AS_ESM } from 'ts-jest/dist/constants'
34

45
test('presets', () => {
5-
const presetKeys = ['transform']
6-
expect(Object.keys(presets.defaults)).toEqual(presetKeys)
7-
expect(Object.keys(presets.jsWithBabel)).toEqual(presetKeys)
8-
expect(Object.keys(presets.jsWithTs)).toEqual(presetKeys)
6+
expect(presets.defaults).toEqual({
7+
transform: {
8+
'^.+\\.tsx?$': 'ts-jest',
9+
},
10+
})
11+
expect(presets.defaultsESM).toEqual({
12+
extensionsToTreatAsEsm: [...TS_EXT_TO_TREAT_AS_ESM],
13+
transform: {
14+
'^.+\\.tsx?$': 'ts-jest',
15+
},
16+
})
17+
expect(presets.jsWithTs).toEqual({
18+
transform: {
19+
'^.+\\.[tj]sx?$': 'ts-jest',
20+
},
21+
})
22+
expect(presets.jsWithTsESM).toEqual({
23+
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
24+
transform: {
25+
'^.+\\.m?[tj]sx?$': 'ts-jest',
26+
},
27+
})
28+
expect(presets.jsWithBabel).toEqual({
29+
transform: {
30+
'^.+\\.tsx?$': 'ts-jest',
31+
'^.+\\.jsx?$': 'babel-jest',
32+
},
33+
})
34+
expect(presets.jsWithBabelESM).toEqual({
35+
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
36+
transform: {
37+
'^.+\\.tsx?$': 'ts-jest',
38+
'^.+\\.m?[j]sx?$': 'babel-jest',
39+
},
40+
})
941
})

e2e/__cases__/ts-jest-checks/index.spec.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

e2e/__tests__/__snapshots__/jest-presets.test.ts.snap

Lines changed: 0 additions & 35 deletions
This file was deleted.

e2e/__tests__/jest-presets.test.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
1-
import { allValidPackageSets, PackageSets } from '../__helpers__/templates'
1+
import { PackageSets } from '../__helpers__/templates'
22
import { configureTestCase } from '../__helpers__/test-case'
33

4-
// 'ts-jest' is tested in almost all test cases
5-
// 'ts-jest/presets/default' is an alias of the above
6-
// 'ts-jest/presets/js-with-ts' is tested in allow-js.test.ts
7-
8-
describe('ts-jest/presets/js-with-babel', () => {
9-
const testCase = configureTestCase('preset-with-babel', { jestConfig: { preset: 'ts-jest/presets/js-with-babel' } })
10-
11-
testCase.runWithTemplates([PackageSets.default], 1, (runTest, { testLabel }) => {
12-
it(testLabel, () => {
13-
const result = runTest()
14-
expect(result.status).toBe(1)
15-
expect(result.stderr).toMatch(/(Couldn't|Cannot) find (preset|module) ["']@babel\/preset-env["']/)
16-
})
17-
})
18-
19-
testCase.runWithTemplates([PackageSets.babel7, PackageSets.babel7StringConfig], 0, (runTest, { testLabel }) => {
20-
it(testLabel, () => {
21-
const result = runTest()
22-
expect(result.status).toBe(0)
23-
expect(result).toMatchSnapshot()
24-
})
25-
})
26-
})
27-
284
describe('ts-jest all presets', () => {
295
const testCase = configureTestCase('presets')
306

31-
testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => {
7+
testCase.runWithTemplates([PackageSets.default], 0, (runTest, { testLabel }) => {
328
it(testLabel, () => {
339
const result = runTest()
3410
expect(result.status).toBe(0)

e2e/__tests__/ts-jest-checks.test.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

presets/default-esm/jest-preset.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('..').defaultsESM

presets/index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1+
const { JS_EXT_TO_TREAT_AS_ESM, TS_EXT_TO_TREAT_AS_ESM } = require('../dist/constants')
12
const { createJestPreset } = require('../dist/presets/create-jest-preset')
23

34
module.exports = {
45
get defaults() {
56
return createJestPreset()
67
},
8+
get defaultsESM() {
9+
return createJestPreset(false, { extensionsToTreatAsEsm: TS_EXT_TO_TREAT_AS_ESM })
10+
},
711
get jsWithTs() {
8-
return createJestPreset({ allowJs: true })
12+
return createJestPreset(true)
13+
},
14+
get jsWithTsESM() {
15+
return createJestPreset(true, { extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM] })
916
},
1017
get jsWithBabel() {
11-
return createJestPreset(
12-
{ allowJs: false },
13-
{
14-
transform: {
15-
'^.+\\.jsx?$': 'babel-jest',
16-
},
17-
}
18-
)
18+
return createJestPreset(false, {
19+
transform: {
20+
'^.+\\.jsx?$': 'babel-jest',
21+
},
22+
})
23+
},
24+
get jsWithBabelESM() {
25+
return createJestPreset(false, {
26+
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
27+
transform: {
28+
'^.+\\.m?[j]sx?$': 'babel-jest',
29+
},
30+
})
1931
},
2032
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('..').jsWithBabelESM

0 commit comments

Comments
 (0)