From 535df6fe65b8c2bad9004e1efd617453b9ebd3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C3=B3dkiewicz?= Date: Mon, 4 Dec 2023 17:29:22 +0100 Subject: [PATCH 1/4] fix: allow modern moduleResolution in project tsconfig This small change fixes an issue that makes using jest impossible with inside a TypeScript project which is working on NodeNext, Node16 or Bundler moduleResolution setting. The default behaviour of ts-node is to read the default tsconfig.json file from the project. With a hardcoded option of "module: CommonJs" the TypeScript compiler will throw an error, because the modern moduleResolution options used in the project are not compatible with the hardcoded module value. The only way to use jest in such a repo is to change the jest.config.ts file into a JS one (or pass the options in a different way), so that the code responsible of instantiating ts-node is not invoked. This commit fixes the issue by providing a missing complementary option, moduleResolution: Node, which will work perfectly with module: CommonJs and will not be overridden by any project-specific value in tsconfig.json. --- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 84ec03e6ccc9..7bfef852dbe8 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -119,6 +119,7 @@ async function registerTsNode(): Promise { return tsNode.register({ compilerOptions: { module: 'CommonJS', + moduleResolution: 'Node', }, moduleTypes: { '**': 'cjs', From d115bb826518f0c154b1279f533c7f3249d1e4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C3=B3dkiewicz?= Date: Thu, 2 May 2024 10:39:31 +0200 Subject: [PATCH 2/4] chore: rename Node to Node10 This is the new preferred name of the old "Node" option. Co-Authored-By: Michael Faith <8071845+michaelfaith@users.noreply.github.com> --- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 7bfef852dbe8..01ddb602dd07 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -119,7 +119,7 @@ async function registerTsNode(): Promise { return tsNode.register({ compilerOptions: { module: 'CommonJS', - moduleResolution: 'Node', + moduleResolution: 'Node10', }, moduleTypes: { '**': 'cjs', From 41a17e5d72fdecbc04cf69965251b9a5eef31b1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C3=B3dkiewicz?= Date: Wed, 15 May 2024 22:10:38 +0200 Subject: [PATCH 3/4] chore: add integration test for Node16 without imports --- e2e/__tests__/typescriptConfigFile.test.ts | 18 +++++++++++++++++- .../modern-module-resolution/__tests__/test.js | 10 ++++++++++ .../modern-module-resolution/jest.config.ts | 12 ++++++++++++ .../modern-module-resolution/package.json | 3 +++ .../modern-module-resolution/tsconfig.json | 8 ++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 e2e/typescript-config/modern-module-resolution/__tests__/test.js create mode 100644 e2e/typescript-config/modern-module-resolution/jest.config.ts create mode 100644 e2e/typescript-config/modern-module-resolution/package.json create mode 100644 e2e/typescript-config/modern-module-resolution/tsconfig.json diff --git a/e2e/__tests__/typescriptConfigFile.test.ts b/e2e/__tests__/typescriptConfigFile.test.ts index df0e316c5bd3..d857e6bfc1d7 100644 --- a/e2e/__tests__/typescriptConfigFile.test.ts +++ b/e2e/__tests__/typescriptConfigFile.test.ts @@ -8,7 +8,7 @@ import {tmpdir} from 'os'; import * as path from 'path'; import {cleanup, writeFiles} from '../Utils'; -import runJest from '../runJest'; +import runJest, {getConfig} from '../runJest'; const DIR = path.resolve(tmpdir(), 'typescript-config-file'); @@ -107,3 +107,19 @@ test('works with multiple typescript configs that import something', () => { expect(exitCode).toBe(0); expect(stdout).toBe(''); }); + +test("works with single typescript config that does not import anything with project's moduleResolution set to Node16", () => { + const {configs} = getConfig( + 'typescript-config/modern-module-resolution', + [], + { + skipPkgJsonCheck: true, + }, + ); + + expect(configs).toHaveLength(1); + expect(configs[0].displayName).toEqual({ + color: 'white', + name: 'Config from modern ts file', + }); +}); diff --git a/e2e/typescript-config/modern-module-resolution/__tests__/test.js b/e2e/typescript-config/modern-module-resolution/__tests__/test.js new file mode 100644 index 000000000000..fc395e3a0f4a --- /dev/null +++ b/e2e/typescript-config/modern-module-resolution/__tests__/test.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +test('dummy test', () => { + expect(1).toBe(1); +}); diff --git a/e2e/typescript-config/modern-module-resolution/jest.config.ts b/e2e/typescript-config/modern-module-resolution/jest.config.ts new file mode 100644 index 000000000000..995821a204f5 --- /dev/null +++ b/e2e/typescript-config/modern-module-resolution/jest.config.ts @@ -0,0 +1,12 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +const config = { + displayName: 'Config from modern ts file', + testEnvironment: 'node', +}; +export default config; diff --git a/e2e/typescript-config/modern-module-resolution/package.json b/e2e/typescript-config/modern-module-resolution/package.json new file mode 100644 index 000000000000..5bbefffbabee --- /dev/null +++ b/e2e/typescript-config/modern-module-resolution/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/e2e/typescript-config/modern-module-resolution/tsconfig.json b/e2e/typescript-config/modern-module-resolution/tsconfig.json new file mode 100644 index 000000000000..bdf27dc03284 --- /dev/null +++ b/e2e/typescript-config/modern-module-resolution/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "Node16", + "moduleResolution": "Node16", + "strict": true + } +} From 350f07832dc0bf5f8a08524c4d88daa7850db57f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C3=B3dkiewicz?= Date: Mon, 20 May 2024 21:38:54 +0200 Subject: [PATCH 4/4] chore: add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45b0f703e4bc..21d59d56382d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ - `[jest-config]` Support `testTimeout` in project config ([#14697](https://github.com/jestjs/jest/pull/14697)) - `[jest-config]` Support `coverageReporters` in project config ([#14697](https://github.com/jestjs/jest/pull/14830)) - `[jest-config]` Allow `reporters` in project config ([#14768](https://github.com/jestjs/jest/pull/14768)) +- `[jest-config]` Allow Node16/NodeNext/Bundler `moduleResolution` in project's tsconfig ([#14739](https://github.com/jestjs/jest/pull/14739)) - `[jest-each]` Allow `$keypath` templates with `null` or `undefined` values ([#14831](https://github.com/jestjs/jest/pull/14831)) - `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408)) - `[@jest/expect-utils]` [**BREAKING**] exclude non-enumerable in object matching ([#14670](https://github.com/jestjs/jest/pull/14670))