Skip to content

Commit

Permalink
feat(jest-config): add support for preset written in ESM (#11200)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl authored Mar 15, 2021
1 parent 708540e commit 75e94cc
Show file tree
Hide file tree
Showing 18 changed files with 262 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `[jest-circus, jest-jasmine2]` [**BREAKING**] Fail the test instead of just warning when describe returns a value ([#10947](https://github.com/facebook/jest/pull/10947))
- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874))
- `[jest-config]` [**BREAKING**] Use `jest-circus` as default test runner ([#10686](https://github.com/facebook/jest/pull/10686))
- `[jest-config]` Add support for `preset` written in ESM ([#11200](https://github.com/facebook/jest/pull/11200))
- `[jest-config, jest-runtime]` Support ESM for files other than `.js` and `.mjs` ([#10823](https://github.com/facebook/jest/pull/10823))
- `[jest-config, jest-runtime]` [**BREAKING**] Use "modern" implementation as default for fake timers ([#10874](https://github.com/facebook/jest/pull/10874) & [#11197](https://github.com/facebook/jest/pull/11197))
- `[jest-core]` make `TestWatcher` extend `emittery` ([#10324](https://github.com/facebook/jest/pull/10324))
Expand Down
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ Specifies notification mode. Requires `notify: true`.

Default: `undefined`

A preset that is used as a base for Jest's configuration. A preset should point to an npm module that has a `jest-preset.json` or `jest-preset.js` file at the root.
A preset that is used as a base for Jest's configuration. A preset should point to an npm module that has a `jest-preset.json`, `jest-preset.js`, `jest-preset.cjs` or `jest-preset.mjs` file at the root.

For example, this preset `foo-bar/jest-preset.js` will be configured as follows:

Expand Down
14 changes: 12 additions & 2 deletions e2e/__tests__/presets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
* LICENSE file in the root directory of this source tree.
*/

import {onNodeVersions} from '@jest/test-utils';
import runJest from '../runJest';

test('supports json preset', () => {
const result = runJest('presets/json');
expect(result.exitCode).toBe(0);
});

test('supports js preset', () => {
const result = runJest('presets/js');
test.each(['js', 'cjs'])('supports %s preset', presetDir => {
const result = runJest(`presets/${presetDir}`);

expect(result.exitCode).toBe(0);
});

onNodeVersions('^12.17.0 || >=13.2.0', () => {
test.each(['mjs', 'js-type-module'])('supports %s preset', presetDir => {
const result = runJest(`presets/${presetDir}`);

expect(result.exitCode).toBe(0);
});
});
11 changes: 11 additions & 0 deletions e2e/presets/cjs/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

test('load file mapped by cjs preset', () => {
expect(require('./test.foo')).toEqual(42);
});
12 changes: 12 additions & 0 deletions e2e/presets/cjs/node_modules/jest-preset-cjs/jest-preset.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions e2e/presets/cjs/node_modules/jest-preset-cjs/mapper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions e2e/presets/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"preset": "jest-preset-cjs"
}
}
11 changes: 11 additions & 0 deletions e2e/presets/js-type-module/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

test('load file mapped by js preset', () => {
expect(require('./test.foo')).toEqual(42);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions e2e/presets/js-type-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"preset": "jest-preset-js-type-module"
}
}
11 changes: 11 additions & 0 deletions e2e/presets/mjs/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

test('load file mapped by mjs preset', () => {
expect(require('./test.foo')).toEqual(42);
});
12 changes: 12 additions & 0 deletions e2e/presets/mjs/node_modules/jest-preset-mjs/jest-preset.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions e2e/presets/mjs/node_modules/jest-preset-mjs/mapper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions e2e/presets/mjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"preset": "jest-preset-mjs"
}
}
Loading

0 comments on commit 75e94cc

Please sign in to comment.