diff --git a/CHANGELOG.md b/CHANGELOG.md index efa038f928b6..c5cdc9aebf2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/docs/Configuration.md b/docs/Configuration.md index 7f9fa35a1b43..8835cc19c459 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -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. diff --git a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap index 4c7058473159..cd1ac20b6546 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -30,6 +30,8 @@ exports[`--showConfig outputs config info and exits 1`] = ` ], "moduleFileExtensions": [ "js", + "mjs", + "cjs", "jsx", "ts", "tsx", diff --git a/e2e/__tests__/__snapshots__/testMatch.test.ts.snap b/e2e/__tests__/__snapshots__/testMatch.test.ts.snap new file mode 100644 index 000000000000..ed8312fe8a59 --- /dev/null +++ b/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: <> +Ran all test suites." +`; diff --git a/e2e/__tests__/testMatch.test.ts b/e2e/__tests__/testMatch.test.ts new file mode 100644 index 000000000000..60b10ca627af --- /dev/null +++ b/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(); +}); diff --git a/e2e/test-match/package.json b/e2e/test-match/package.json new file mode 100644 index 000000000000..3d6779c71dd7 --- /dev/null +++ b/e2e/test-match/package.json @@ -0,0 +1,7 @@ +{ + "jest": { + "testMatch": [ + "**/test-suites/*.?js" + ] + } +} diff --git a/e2e/test-match/test-suites/sample-suite.mjs b/e2e/test-match/test-suites/sample-suite.mjs new file mode 100644 index 000000000000..0d0703ef8e57 --- /dev/null +++ b/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); +}); diff --git a/e2e/test-match/test-suites/sample-suite2.cjs b/e2e/test-match/test-suites/sample-suite2.cjs new file mode 100644 index 000000000000..bb80acb6f879 --- /dev/null +++ b/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); +}); diff --git a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap index 157419444c3d..fbb7b4b2ad05 100644 --- a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap +++ b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap @@ -191,6 +191,8 @@ module.exports = { // An array of file extensions your modules use // moduleFileExtensions: [ // "js", + // "mjs", + // "cjs", // "jsx", // "ts", // "tsx", diff --git a/packages/jest-config/src/Defaults.ts b/packages/jest-config/src/Defaults.ts index 9cc30e2d09e3..45cd6b7481d8 100644 --- a/packages/jest-config/src/Defaults.ts +++ b/packages/jest-config/src/Defaults.ts @@ -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, diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index 5757431123ed..53e833d76ff2 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -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$': '/node_modules/react', }, diff --git a/packages/jest-config/src/__tests__/normalize.test.ts b/packages/jest-config/src/__tests__/normalize.test.ts index ccca146c1c54..5139b873215c 100644 --- a/packages/jest-config/src/__tests__/normalize.test.ts +++ b/packages/jest-config/src/__tests__/normalize.test.ts @@ -1668,6 +1668,8 @@ describe('moduleFileExtensions', () => { expect(options.moduleFileExtensions).toEqual([ 'js', + 'mjs', + 'cjs', 'jsx', 'ts', 'tsx', diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index bfd425984283..b1ab7a935f45 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -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) {