From 5ac2828d85a05c23053eb9d33b4a3a994f5f180f Mon Sep 17 00:00:00 2001 From: feng Date: Tue, 15 Mar 2022 09:53:38 +0800 Subject: [PATCH 01/12] fix(jest-config): add `mjs` to default `moduleFileExtensions` config --- CHANGELOG.md | 1 + docs/Configuration.md | 2 +- packages/jest-config/src/Defaults.ts | 2 +- packages/jest-config/src/ValidConfig.ts | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efa038f928b6..96d346271e78 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]` Add `mjs` 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..1094292db939 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", "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/packages/jest-config/src/Defaults.ts b/packages/jest-config/src/Defaults.ts index 9cc30e2d09e3..466c08eb9eeb 100644 --- a/packages/jest-config/src/Defaults.ts +++ b/packages/jest-config/src/Defaults.ts @@ -44,7 +44,7 @@ const defaultOptions: Config.DefaultOptions = { maxConcurrency: 5, maxWorkers: '50%', moduleDirectories: ['node_modules'], - moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json', 'node'], + moduleFileExtensions: ['js', 'mjs', '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..2e985b3f389e 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -74,7 +74,7 @@ const initialOptions: Config.InitialOptions = { maxConcurrency: 5, maxWorkers: '50%', moduleDirectories: ['node_modules'], - moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'], + moduleFileExtensions: ['js', 'mjs', 'json', 'jsx', 'ts', 'tsx', 'node'], moduleNameMapper: { '^React$': '/node_modules/react', }, From d5a05e9aa1eace2f3a3e3edb179deb6b63c5d7ea Mon Sep 17 00:00:00 2001 From: feng Date: Tue, 15 Mar 2022 22:29:08 +0800 Subject: [PATCH 02/12] update snapshot --- e2e/__tests__/__snapshots__/showConfig.test.ts.snap | 1 + .../jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap | 1 + 2 files changed, 2 insertions(+) diff --git a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap index 4c7058473159..f4688f6c4eec 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -30,6 +30,7 @@ exports[`--showConfig outputs config info and exits 1`] = ` ], "moduleFileExtensions": [ "js", + "mjs", "jsx", "ts", "tsx", 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..0691db3c7896 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,7 @@ module.exports = { // An array of file extensions your modules use // moduleFileExtensions: [ // "js", + // "mjs", // "jsx", // "ts", // "tsx", From a789e11e837ecfdbf168b7b7e4ed317f4b7dfc8c Mon Sep 17 00:00:00 2001 From: feng Date: Tue, 15 Mar 2022 22:36:54 +0800 Subject: [PATCH 03/12] fix test --- packages/jest-config/src/__tests__/normalize.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-config/src/__tests__/normalize.test.ts b/packages/jest-config/src/__tests__/normalize.test.ts index ccca146c1c54..9c52542dbded 100644 --- a/packages/jest-config/src/__tests__/normalize.test.ts +++ b/packages/jest-config/src/__tests__/normalize.test.ts @@ -1668,6 +1668,7 @@ describe('moduleFileExtensions', () => { expect(options.moduleFileExtensions).toEqual([ 'js', + 'mjs', 'jsx', 'ts', 'tsx', From 6bd00da4882a52e64e8003e6c94bd8573596d06a Mon Sep 17 00:00:00 2001 From: feng Date: Wed, 16 Mar 2022 23:15:24 +0800 Subject: [PATCH 04/12] exclude mjs to not tranpile --- packages/jest-transform/src/ScriptTransformer.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index bfd425984283..553e621185e2 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -759,7 +759,9 @@ class ScriptTransformer { } }, { - exts: this._config.moduleFileExtensions.map(ext => `.${ext}`), + exts: this._config.moduleFileExtensions + .filter(ext => ext !== 'mjs') + .map(ext => `.${ext}`), ignoreNodeModules: false, matcher: filename => { if (transforming) { @@ -799,6 +801,13 @@ class ScriptTransformer { shouldTransform(filename: string): boolean { const ignoreRegexp = this._cache.ignorePatternsRegExp; const isIgnored = ignoreRegexp ? ignoreRegexp.test(filename) : false; + console.log( + 'should transform', + filename, + this._config.transform, + ignoreRegexp, + this._config.transform.length !== 0 && !isIgnored, + ); return this._config.transform.length !== 0 && !isIgnored; } From 0ecbe5a5f2f8f2792fb4d9502617f22a20e92831 Mon Sep 17 00:00:00 2001 From: Feng Yu Date: Thu, 17 Mar 2022 08:15:51 +0800 Subject: [PATCH 05/12] Update ScriptTransformer.ts --- packages/jest-transform/src/ScriptTransformer.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 553e621185e2..1864319361c2 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -801,13 +801,6 @@ class ScriptTransformer { shouldTransform(filename: string): boolean { const ignoreRegexp = this._cache.ignorePatternsRegExp; const isIgnored = ignoreRegexp ? ignoreRegexp.test(filename) : false; - console.log( - 'should transform', - filename, - this._config.transform, - ignoreRegexp, - this._config.transform.length !== 0 && !isIgnored, - ); return this._config.transform.length !== 0 && !isIgnored; } From 70522ef8e0cf6b9260adcb16426ce05c8124cd39 Mon Sep 17 00:00:00 2001 From: feng Date: Tue, 22 Mar 2022 19:50:44 +0800 Subject: [PATCH 06/12] add cjs --- docs/Configuration.md | 2 +- e2e/__tests__/__snapshots__/showConfig.test.ts.snap | 1 + .../init/__tests__/__snapshots__/init.test.js.snap | 1 + packages/jest-config/src/Defaults.ts | 11 ++++++++++- packages/jest-config/src/ValidConfig.ts | 11 ++++++++++- packages/jest-config/src/__tests__/normalize.test.ts | 1 + 6 files changed, 24 insertions(+), 3 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 1094292db939..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", "mjs", "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 f4688f6c4eec..cd1ac20b6546 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -31,6 +31,7 @@ exports[`--showConfig outputs config info and exits 1`] = ` "moduleFileExtensions": [ "js", "mjs", + "cjs", "jsx", "ts", "tsx", 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 0691db3c7896..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 @@ -192,6 +192,7 @@ module.exports = { // moduleFileExtensions: [ // "js", // "mjs", + // "cjs", // "jsx", // "ts", // "tsx", diff --git a/packages/jest-config/src/Defaults.ts b/packages/jest-config/src/Defaults.ts index 466c08eb9eeb..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', 'mjs', '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 2e985b3f389e..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', 'mjs', '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 9c52542dbded..5139b873215c 100644 --- a/packages/jest-config/src/__tests__/normalize.test.ts +++ b/packages/jest-config/src/__tests__/normalize.test.ts @@ -1669,6 +1669,7 @@ describe('moduleFileExtensions', () => { expect(options.moduleFileExtensions).toEqual([ 'js', 'mjs', + 'cjs', 'jsx', 'ts', 'tsx', From 684dfef59bf2818e0e099cb9af176af4b3004db7 Mon Sep 17 00:00:00 2001 From: feng Date: Tue, 22 Mar 2022 19:51:50 +0800 Subject: [PATCH 07/12] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96d346271e78..752bb5446590 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,7 +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]` Add `mjs` to default `moduleFileExtensions` config ([12578](https://github.com/facebook/jest/pull/12578)) +- `[jest-config]` 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)) From 8cbe3f3dc4409383fa935a80518c5ce1d80496fb Mon Sep 17 00:00:00 2001 From: feng Date: Tue, 22 Mar 2022 20:15:21 +0800 Subject: [PATCH 08/12] add e2e test --- e2e/__tests__/__snapshots__/testMatch.test.ts.snap | 9 +++++++++ e2e/__tests__/testMatch.test.ts | 9 +++++++++ e2e/test-match/package.json | 5 +++++ e2e/test-match/test-suites/sample-suite.mjs | 3 +++ e2e/test-match/test-suites/sample-suite2.cjs | 3 +++ 5 files changed, 29 insertions(+) create mode 100644 e2e/__tests__/__snapshots__/testMatch.test.ts.snap create mode 100644 e2e/__tests__/testMatch.test.ts create mode 100644 e2e/test-match/package.json create mode 100644 e2e/test-match/test-suites/sample-suite.mjs create mode 100644 e2e/test-match/test-suites/sample-suite2.cjs 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..f5bd3c8ac396 --- /dev/null +++ b/e2e/__tests__/testMatch.test.ts @@ -0,0 +1,9 @@ +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..36f34d232c36 --- /dev/null +++ b/e2e/test-match/package.json @@ -0,0 +1,5 @@ +{ + "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..6a9ca71b0ee0 --- /dev/null +++ b/e2e/test-match/test-suites/sample-suite.mjs @@ -0,0 +1,3 @@ +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..c512c78ee5fa --- /dev/null +++ b/e2e/test-match/test-suites/sample-suite2.cjs @@ -0,0 +1,3 @@ +test('cjs extension', () => { + expect(1).toBe(1); +}); From 5c1acb1441bce43c3c6a37166932e37e51f39a67 Mon Sep 17 00:00:00 2001 From: feng Date: Tue, 22 Mar 2022 20:44:57 +0800 Subject: [PATCH 09/12] add copyright header --- e2e/__tests__/testMatch.test.ts | 7 +++++++ e2e/test-match/test-suites/sample-suite.mjs | 7 +++++++ e2e/test-match/test-suites/sample-suite2.cjs | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/e2e/__tests__/testMatch.test.ts b/e2e/__tests__/testMatch.test.ts index f5bd3c8ac396..60b10ca627af 100644 --- a/e2e/__tests__/testMatch.test.ts +++ b/e2e/__tests__/testMatch.test.ts @@ -1,3 +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. + */ + import {extractSummary} from '../Utils'; import runJest from '../runJest'; diff --git a/e2e/test-match/test-suites/sample-suite.mjs b/e2e/test-match/test-suites/sample-suite.mjs index 6a9ca71b0ee0..0d0703ef8e57 100644 --- a/e2e/test-match/test-suites/sample-suite.mjs +++ b/e2e/test-match/test-suites/sample-suite.mjs @@ -1,3 +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 index c512c78ee5fa..bb80acb6f879 100644 --- a/e2e/test-match/test-suites/sample-suite2.cjs +++ b/e2e/test-match/test-suites/sample-suite2.cjs @@ -1,3 +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); }); From 469cec687cd4d2d8ceb41b18f50452ab3642e498 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Mar 2022 11:55:49 +0100 Subject: [PATCH 10/12] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 752bb5446590..c5cdc9aebf2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,7 +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]` Add `mjs` and `cjs` to default `moduleFileExtensions` config ([12578](https://github.com/facebook/jest/pull/12578)) +- `[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)) From 8ea3283f3a072bf9111596a4f5cbc65dc4859ece Mon Sep 17 00:00:00 2001 From: feng Date: Mon, 28 Mar 2022 20:30:11 +0800 Subject: [PATCH 11/12] add comment --- packages/jest-transform/src/ScriptTransformer.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 1864319361c2..b1ab7a935f45 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -759,6 +759,7 @@ class ScriptTransformer { } }, { + // Exclude `mjs` extension when addHook because pirates don't support hijack es module exts: this._config.moduleFileExtensions .filter(ext => ext !== 'mjs') .map(ext => `.${ext}`), From be8ad015d4d36261bc70b48aef1a750a8489f42d Mon Sep 17 00:00:00 2001 From: feng Date: Tue, 29 Mar 2022 09:28:46 +0800 Subject: [PATCH 12/12] run prettier --- e2e/test-match/package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/e2e/test-match/package.json b/e2e/test-match/package.json index 36f34d232c36..3d6779c71dd7 100644 --- a/e2e/test-match/package.json +++ b/e2e/test-match/package.json @@ -1,5 +1,7 @@ { "jest": { - "testMatch": ["**/test-suites/*.?js"] + "testMatch": [ + "**/test-suites/*.?js" + ] } }