Skip to content

Commit

Permalink
Merge pull request #757 from huafu/testMatch-and-testRegex
Browse files Browse the repository at this point in the history
testMatch and testRegex + presets
  • Loading branch information
huafu committed Sep 26, 2018
2 parents 0534889 + a4bbdcd commit e765875
Show file tree
Hide file tree
Showing 20 changed files with 411 additions and 49 deletions.
30 changes: 25 additions & 5 deletions docs/user/config/index.md
Expand Up @@ -6,16 +6,28 @@ The later is preferred since it's more customizable, but it depends on your need

## Jest preset

### The 3 presets

`ts-jest` comes with 3 presets, covering most of 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 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. |
| `ts-jest/presets/js-with-babel` | TypeScript files will be handled by `ts-jest`, and JavaScript files will be handled by `babel-jest`. |

### Basic usage

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):
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):

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

```js
// jest.config.js
module.exports = {
// [...]
// Replace `ts-jest` with the preset you want to use
// from the above list
preset: 'ts-jest'
};
```
Expand All @@ -27,27 +39,35 @@ module.exports = {
{
// [...]
"jest": {
// Replace `ts-jest` with the preset you want to use
// from the above list
"preset": "ts-jest"
}
}
```

</div></div>

**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.

### Advanced

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

```js
// jest.config.js
const { jestPreset: tsJestPreset } = require('ts-jest');
const tsJestPresets = require('ts-jest/presets');

const preset = tsJestPresets.defaults
// const preset = tsJestPresets.jsWithTs
// const preset = tsJestPresets.jsWithBabel

module.exports = {
// [...]
transform: {
...tsJestPreset.transform,
...preset.transform,
// [...]
}
}
Expand Down
5 changes: 5 additions & 0 deletions e2e/__cases__/allow-js/esm.spec.js
@@ -0,0 +1,5 @@
import * as bar from './bar'

test('esm', () => {
expect(bar).toBe('BAR!')
})
5 changes: 5 additions & 0 deletions e2e/__cases__/preset-with-babel/.babelrc
@@ -0,0 +1,5 @@
{
"presets": [
"@babel/preset-env"
]
}
3 changes: 3 additions & 0 deletions e2e/__cases__/preset-with-babel/main.spec.js
@@ -0,0 +1,3 @@
test('spread', () => {
expect({ ...{ bar: 'foo' }, foo: 'bar' }).toEqual({ foo: 'bar', bar: 'foo' })
})
6 changes: 6 additions & 0 deletions e2e/__helpers__/templates.ts
Expand Up @@ -14,3 +14,9 @@ export const allValidPackageSets = [
PackageSets.jest22,
PackageSets.typescript2_7,
]
export const allPackageSetsWithPreset = [
PackageSets.default,
PackageSets.babel6,
PackageSets.babel7,
PackageSets.typescript2_7,
]
78 changes: 73 additions & 5 deletions e2e/__tests__/__snapshots__/allow-js.test.ts.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Allow JS test should pass using template "default" 1`] = `
exports[`using babel-jest for js files should pass using template "default" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -17,7 +17,7 @@ exports[`Allow JS test should pass using template "default" 1`] = `
================================================================================
`;

exports[`Allow JS test should pass using template "with-babel-6" 1`] = `
exports[`using babel-jest for js files should pass using template "with-babel-6" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -34,7 +34,7 @@ exports[`Allow JS test should pass using template "with-babel-6" 1`] = `
================================================================================
`;

exports[`Allow JS test should pass using template "with-babel-7" 1`] = `
exports[`using babel-jest for js files should pass using template "with-babel-7" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -51,7 +51,7 @@ exports[`Allow JS test should pass using template "with-babel-7" 1`] = `
================================================================================
`;

exports[`Allow JS test should pass using template "with-jest-22" 1`] = `
exports[`using babel-jest for js files should pass using template "with-jest-22" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -68,7 +68,7 @@ exports[`Allow JS test should pass using template "with-jest-22" 1`] = `
================================================================================
`;

exports[`Allow JS test should pass using template "with-typescript-2-7" 1`] = `
exports[`using babel-jest for js files should pass using template "with-typescript-2-7" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -84,3 +84,71 @@ exports[`Allow JS test should pass using template "with-typescript-2-7" 1`] = `
Ran all test suites.
================================================================================
`;

exports[`using ts-jest for js files should pass using template "default" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
PASS ./esm.spec.js
√ esm
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`using ts-jest for js files should pass using template "with-babel-6" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
PASS ./esm.spec.js
√ esm
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`using ts-jest for js files should pass using template "with-babel-7" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
PASS ./esm.spec.js
√ esm
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`using ts-jest for js files should pass using template "with-typescript-2-7" 1`] = `
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
PASS ./esm.spec.js
√ esm
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;
26 changes: 23 additions & 3 deletions e2e/__tests__/allow-js.test.ts
@@ -1,8 +1,10 @@
import { allValidPackageSets } from '../__helpers__/templates'
import { allPackageSetsWithPreset, allValidPackageSets } from '../__helpers__/templates'
import { configureTestCase } from '../__helpers__/test-case'

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

testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => {
it(testLabel, () => {
Expand All @@ -12,3 +14,21 @@ describe('Allow JS test', () => {
})
})
})

describe('using ts-jest for js files', () => {
const testCase = configureTestCase('allow-js', {
jestConfig: {
preset: 'ts-jest/presets/js-with-ts',
testMatch: null,
testRegex: 'esm\\.spec\\.[jt]s$',
},
})

testCase.runWithTemplates(allPackageSetsWithPreset, 0, (runTest, { testLabel }) => {
it(testLabel, () => {
const result = runTest()
expect(result.status).toBe(0)
expect(result).toMatchSnapshot()
})
})
})
18 changes: 18 additions & 0 deletions e2e/__tests__/jest-presets.test.ts
@@ -0,0 +1,18 @@
import { allPackageSetsWithPreset } 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(allPackageSetsWithPreset, 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["']/)
})
})
})
5 changes: 1 addition & 4 deletions jest-preset.js
@@ -1,4 +1 @@
const createJestPreset = require('./dist/config/create-jest-preset')
.createJestPreset

module.exports = createJestPreset()
module.exports = require('./presets/default/jest-preset')
1 change: 1 addition & 0 deletions presets/create.js
@@ -0,0 +1 @@
module.exports = require('../dist/config/create-jest-preset').createJestPreset
1 change: 1 addition & 0 deletions presets/default/jest-preset.js
@@ -0,0 +1 @@
module.exports = require('..').defaults
5 changes: 5 additions & 0 deletions presets/index.d.ts
@@ -0,0 +1,5 @@
import { TsJestPresets } from '../dist/types'

export const defaults: TsJesPresets
export const jsWithTs: TsJesPresets
export const jsWithBabel: TsJesPresets
13 changes: 13 additions & 0 deletions presets/index.js
@@ -0,0 +1,13 @@
const create = require('./create')

module.exports = {
get defaults() { return create() },
get jsWithTs() { return create({ allowJs: true }) },
get jsWithBabel() {
return create({ allowJs: false }, {
transform: {
'^.+\\.jsx?$': 'babel-jest',
},
})
},
}
1 change: 1 addition & 0 deletions presets/js-with-babel/jest-preset.js
@@ -0,0 +1 @@
module.exports = require('..').jsWithBabel
1 change: 1 addition & 0 deletions presets/js-with-ts/jest-preset.js
@@ -0,0 +1 @@
module.exports = require('..').jsWithTs

0 comments on commit e765875

Please sign in to comment.