Skip to content

Commit e765875

Browse files
authored
Merge pull request #757 from huafu/testMatch-and-testRegex
testMatch and testRegex + presets
2 parents 0534889 + a4bbdcd commit e765875

File tree

20 files changed

+411
-49
lines changed

20 files changed

+411
-49
lines changed

docs/user/config/index.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,28 @@ The later is preferred since it's more customizable, but it depends on your need
66

77
## Jest preset
88

9+
### The 3 presets
10+
11+
`ts-jest` comes with 3 presets, covering most of project's base configuration:
12+
13+
| Preset name | Description |
14+
|---|---|
15+
| `ts-jest/presets/default`<br>or `ts-jest` | `ts-jest` will take care of `.ts` and `.tsx` files only, leaving JavaScript files as-is. |
16+
| `ts-jest/presets/js-with-ts` | TypeScript and JavaScript file (`.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. |
17+
| `ts-jest/presets/js-with-babel` | TypeScript files will be handled by `ts-jest`, and JavaScript files will be handled by `babel-jest`. |
18+
919
### Basic usage
1020

11-
In most cases, simply adding `preset: 'ts-jest'` to your Jest config should be enough to start using TypeScript with Jest (assuming you did add `ts-jest` to your dev. dependencies of course):
21+
In most cases, simply setting the `preset` key to the desired preset name in your Jest config should be enough to start using TypeScript with Jest (assuming you did add `ts-jest` to your dev. dependencies of course):
1222

1323
<div class="row"><div class="col-md-6" markdown="block">
1424

1525
```js
1626
// jest.config.js
1727
module.exports = {
1828
// [...]
29+
// Replace `ts-jest` with the preset you want to use
30+
// from the above list
1931
preset: 'ts-jest'
2032
};
2133
```
@@ -27,27 +39,35 @@ module.exports = {
2739
{
2840
// [...]
2941
"jest": {
42+
// Replace `ts-jest` with the preset you want to use
43+
// from the above list
3044
"preset": "ts-jest"
3145
}
3246
}
3347
```
3448

3549
</div></div>
3650

51+
**Note:** presets use `testMatch`, like Jest does in its defaults. If you want to use `testRegex` instead in your configuration, you MUST set `testMatch` to `null` or Jest will bail.
52+
3753
### Advanced
3854

39-
The `ts-jest` preset can also be used with other options.
40-
If you're already using another preset, you might want only some specific settings from the `ts-jest` preset.
55+
Any preset can also be used with other options.
56+
If you're already using another preset, you might want only some specific settings from the chosen `ts-jest` preset.
4157
In this case you'll need to use the JavaScript version of Jest config:
4258

4359
```js
4460
// jest.config.js
45-
const { jestPreset: tsJestPreset } = require('ts-jest');
61+
const tsJestPresets = require('ts-jest/presets');
62+
63+
const preset = tsJestPresets.defaults
64+
// const preset = tsJestPresets.jsWithTs
65+
// const preset = tsJestPresets.jsWithBabel
4666

4767
module.exports = {
4868
// [...]
4969
transform: {
50-
...tsJestPreset.transform,
70+
...preset.transform,
5171
// [...]
5272
}
5373
}

e2e/__cases__/allow-js/esm.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as bar from './bar'
2+
3+
test('esm', () => {
4+
expect(bar).toBe('BAR!')
5+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env"
4+
]
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test('spread', () => {
2+
expect({ ...{ bar: 'foo' }, foo: 'bar' }).toEqual({ foo: 'bar', bar: 'foo' })
3+
})

e2e/__helpers__/templates.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ export const allValidPackageSets = [
1414
PackageSets.jest22,
1515
PackageSets.typescript2_7,
1616
]
17+
export const allPackageSetsWithPreset = [
18+
PackageSets.default,
19+
PackageSets.babel6,
20+
PackageSets.babel7,
21+
PackageSets.typescript2_7,
22+
]

e2e/__tests__/__snapshots__/allow-js.test.ts.snap

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Allow JS test should pass using template "default" 1`] = `
3+
exports[`using babel-jest for js files should pass using template "default" 1`] = `
44
√ jest
55
↳ exit code: 0
66
===[ STDOUT ]===================================================================
@@ -17,7 +17,7 @@ exports[`Allow JS test should pass using template "default" 1`] = `
1717
================================================================================
1818
`;
1919

20-
exports[`Allow JS test should pass using template "with-babel-6" 1`] = `
20+
exports[`using babel-jest for js files should pass using template "with-babel-6" 1`] = `
2121
√ jest
2222
↳ exit code: 0
2323
===[ STDOUT ]===================================================================
@@ -34,7 +34,7 @@ exports[`Allow JS test should pass using template "with-babel-6" 1`] = `
3434
================================================================================
3535
`;
3636

37-
exports[`Allow JS test should pass using template "with-babel-7" 1`] = `
37+
exports[`using babel-jest for js files should pass using template "with-babel-7" 1`] = `
3838
√ jest
3939
↳ exit code: 0
4040
===[ STDOUT ]===================================================================
@@ -51,7 +51,7 @@ exports[`Allow JS test should pass using template "with-babel-7" 1`] = `
5151
================================================================================
5252
`;
5353

54-
exports[`Allow JS test should pass using template "with-jest-22" 1`] = `
54+
exports[`using babel-jest for js files should pass using template "with-jest-22" 1`] = `
5555
√ jest
5656
↳ exit code: 0
5757
===[ STDOUT ]===================================================================
@@ -68,7 +68,7 @@ exports[`Allow JS test should pass using template "with-jest-22" 1`] = `
6868
================================================================================
6969
`;
7070

71-
exports[`Allow JS test should pass using template "with-typescript-2-7" 1`] = `
71+
exports[`using babel-jest for js files should pass using template "with-typescript-2-7" 1`] = `
7272
√ jest
7373
↳ exit code: 0
7474
===[ STDOUT ]===================================================================
@@ -84,3 +84,71 @@ exports[`Allow JS test should pass using template "with-typescript-2-7" 1`] = `
8484
Ran all test suites.
8585
================================================================================
8686
`;
87+
88+
exports[`using ts-jest for js files should pass using template "default" 1`] = `
89+
√ jest
90+
↳ exit code: 0
91+
===[ STDOUT ]===================================================================
92+
93+
===[ STDERR ]===================================================================
94+
PASS ./esm.spec.js
95+
√ esm
96+
97+
Test Suites: 1 passed, 1 total
98+
Tests: 1 passed, 1 total
99+
Snapshots: 0 total
100+
Time: XXs
101+
Ran all test suites.
102+
================================================================================
103+
`;
104+
105+
exports[`using ts-jest for js files should pass using template "with-babel-6" 1`] = `
106+
√ jest
107+
↳ exit code: 0
108+
===[ STDOUT ]===================================================================
109+
110+
===[ STDERR ]===================================================================
111+
PASS ./esm.spec.js
112+
√ esm
113+
114+
Test Suites: 1 passed, 1 total
115+
Tests: 1 passed, 1 total
116+
Snapshots: 0 total
117+
Time: XXs
118+
Ran all test suites.
119+
================================================================================
120+
`;
121+
122+
exports[`using ts-jest for js files should pass using template "with-babel-7" 1`] = `
123+
√ jest
124+
↳ exit code: 0
125+
===[ STDOUT ]===================================================================
126+
127+
===[ STDERR ]===================================================================
128+
PASS ./esm.spec.js
129+
√ esm
130+
131+
Test Suites: 1 passed, 1 total
132+
Tests: 1 passed, 1 total
133+
Snapshots: 0 total
134+
Time: XXs
135+
Ran all test suites.
136+
================================================================================
137+
`;
138+
139+
exports[`using ts-jest for js files should pass using template "with-typescript-2-7" 1`] = `
140+
√ jest
141+
↳ exit code: 0
142+
===[ STDOUT ]===================================================================
143+
144+
===[ STDERR ]===================================================================
145+
PASS ./esm.spec.js
146+
√ esm
147+
148+
Test Suites: 1 passed, 1 total
149+
Tests: 1 passed, 1 total
150+
Snapshots: 0 total
151+
Time: XXs
152+
Ran all test suites.
153+
================================================================================
154+
`;

e2e/__tests__/allow-js.test.ts

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

4-
describe('Allow JS test', () => {
5-
const testCase = configureTestCase('allow-js')
4+
describe('using babel-jest for js files', () => {
5+
const testCase = configureTestCase('allow-js', {
6+
jestConfig: { testMatch: null, testRegex: '(foo|bar)\\.spec\\.[jt]s$' },
7+
})
68

79
testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => {
810
it(testLabel, () => {
@@ -12,3 +14,21 @@ describe('Allow JS test', () => {
1214
})
1315
})
1416
})
17+
18+
describe('using ts-jest for js files', () => {
19+
const testCase = configureTestCase('allow-js', {
20+
jestConfig: {
21+
preset: 'ts-jest/presets/js-with-ts',
22+
testMatch: null,
23+
testRegex: 'esm\\.spec\\.[jt]s$',
24+
},
25+
})
26+
27+
testCase.runWithTemplates(allPackageSetsWithPreset, 0, (runTest, { testLabel }) => {
28+
it(testLabel, () => {
29+
const result = runTest()
30+
expect(result.status).toBe(0)
31+
expect(result).toMatchSnapshot()
32+
})
33+
})
34+
})

e2e/__tests__/jest-presets.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { allPackageSetsWithPreset } from '../__helpers__/templates'
2+
import { configureTestCase } from '../__helpers__/test-case'
3+
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(allPackageSetsWithPreset, 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+
})

jest-preset.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
const createJestPreset = require('./dist/config/create-jest-preset')
2-
.createJestPreset
3-
4-
module.exports = createJestPreset()
1+
module.exports = require('./presets/default/jest-preset')

presets/create.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../dist/config/create-jest-preset').createJestPreset

presets/default/jest-preset.js

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

presets/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { TsJestPresets } from '../dist/types'
2+
3+
export const defaults: TsJesPresets
4+
export const jsWithTs: TsJesPresets
5+
export const jsWithBabel: TsJesPresets

presets/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const create = require('./create')
2+
3+
module.exports = {
4+
get defaults() { return create() },
5+
get jsWithTs() { return create({ allowJs: true }) },
6+
get jsWithBabel() {
7+
return create({ allowJs: false }, {
8+
transform: {
9+
'^.+\\.jsx?$': 'babel-jest',
10+
},
11+
})
12+
},
13+
}

presets/js-with-babel/jest-preset.js

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

presets/js-with-ts/jest-preset.js

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

0 commit comments

Comments
 (0)