Skip to content

Commit

Permalink
fix(jest-config): add mjs and cjs to default `moduleFileExtension…
Browse files Browse the repository at this point in the history
…s` config (#12578)
  • Loading branch information
F3n67u committed Mar 29, 2022
1 parent 0d98094 commit daf583c
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -50,6 +50,7 @@
- `[jest-circus, @jest/types]` Disallow undefined value in `TestContext` type ([#12507](https://github.com/facebook/jest/pull/12507))
- `[jest-config]` Correctly detect CI environment and update snapshots accordingly ([#12378](https://github.com/facebook/jest/pull/12378))
- `[jest-config]` Pass `moduleTypes` to `ts-node` to enforce CJS when transpiling ([#12397](https://github.com/facebook/jest/pull/12397))
- `[jest-config]` [**BREAKING**] Add `mjs` and `cjs` to default `moduleFileExtensions` config ([12578](https://github.com/facebook/jest/pull/12578))
- `[jest-config, jest-haste-map]` Allow searching for tests in `node_modules` by exposing `retainAllFiles` ([#11084](https://github.com/facebook/jest/pull/11084))
- `[jest-core]` [**BREAKING**] Exit with status `1` if no tests are found with `--findRelatedTests` flag ([#12487](https://github.com/facebook/jest/pull/12487))
- `[jest-each]` `%#` is not replaced with index of the test case ([#12517](https://github.com/facebook/jest/pull/12517))
Expand Down
2 changes: 1 addition & 1 deletion docs/Configuration.md
Expand Up @@ -571,7 +571,7 @@ An array of directory names to be searched recursively up from the requiring mod

### `moduleFileExtensions` \[array<string>]

Default: `["js", "jsx", "ts", "tsx", "json", "node"]`
Default: `["js", "mjs", "cjs", "jsx", "ts", "tsx", "json", "node"]`

An array of file extensions your modules use. If you require modules without specifying a file extension, these are the extensions Jest will look for, in left-to-right order.

Expand Down
2 changes: 2 additions & 0 deletions e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Expand Up @@ -30,6 +30,8 @@ exports[`--showConfig outputs config info and exits 1`] = `
],
"moduleFileExtensions": [
"js",
"mjs",
"cjs",
"jsx",
"ts",
"tsx",
Expand Down
9 changes: 9 additions & 0 deletions e2e/__tests__/__snapshots__/testMatch.test.ts.snap
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`testMatch should able to match file with cjs and mjs extension 1`] = `
"Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites."
`;
16 changes: 16 additions & 0 deletions e2e/__tests__/testMatch.test.ts
@@ -0,0 +1,16 @@
/**
* 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.
*/

import {extractSummary} from '../Utils';
import runJest from '../runJest';

it('testMatch should able to match file with cjs and mjs extension', () => {
const result = runJest('test-match');
expect(result.exitCode).toBe(0);
const {summary} = extractSummary(result.stderr);
expect(summary).toMatchSnapshot();
});
7 changes: 7 additions & 0 deletions e2e/test-match/package.json
@@ -0,0 +1,7 @@
{
"jest": {
"testMatch": [
"**/test-suites/*.?js"
]
}
}
10 changes: 10 additions & 0 deletions e2e/test-match/test-suites/sample-suite.mjs
@@ -0,0 +1,10 @@
/**
* 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.
*/

test('mjs extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match/test-suites/sample-suite2.cjs
@@ -0,0 +1,10 @@
/**
* 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.
*/

test('cjs extension', () => {
expect(1).toBe(1);
});
Expand Up @@ -191,6 +191,8 @@ module.exports = {
// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "mjs",
// "cjs",
// "jsx",
// "ts",
// "tsx",
Expand Down
11 changes: 10 additions & 1 deletion packages/jest-config/src/Defaults.ts
Expand Up @@ -44,7 +44,16 @@ const defaultOptions: Config.DefaultOptions = {
maxConcurrency: 5,
maxWorkers: '50%',
moduleDirectories: ['node_modules'],
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json', 'node'],
moduleFileExtensions: [
'js',
'mjs',
'cjs',
'jsx',
'ts',
'tsx',
'json',
'node',
],
moduleNameMapper: {},
modulePathIgnorePatterns: [],
noStackTrace: false,
Expand Down
11 changes: 10 additions & 1 deletion packages/jest-config/src/ValidConfig.ts
Expand Up @@ -74,7 +74,16 @@ const initialOptions: Config.InitialOptions = {
maxConcurrency: 5,
maxWorkers: '50%',
moduleDirectories: ['node_modules'],
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
moduleFileExtensions: [
'js',
'mjs',
'cjs',
'json',
'jsx',
'ts',
'tsx',
'node',
],
moduleNameMapper: {
'^React$': '<rootDir>/node_modules/react',
},
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.ts
Expand Up @@ -1668,6 +1668,8 @@ describe('moduleFileExtensions', () => {

expect(options.moduleFileExtensions).toEqual([
'js',
'mjs',
'cjs',
'jsx',
'ts',
'tsx',
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -759,7 +759,10 @@ class ScriptTransformer {
}
},
{
exts: this._config.moduleFileExtensions.map(ext => `.${ext}`),
// Exclude `mjs` extension when addHook because pirates don't support hijack es module
exts: this._config.moduleFileExtensions
.filter(ext => ext !== 'mjs')
.map(ext => `.${ext}`),
ignoreNodeModules: false,
matcher: filename => {
if (transforming) {
Expand Down

0 comments on commit daf583c

Please sign in to comment.