From 7c462f5e5e3a5714c5541c1a4661246c029005e0 Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Wed, 6 Nov 2024 10:12:35 -0700 Subject: [PATCH] fix(core): projects should work with nodenext set in tsconfig --- .../generators/configuration/configuration.ts | 32 ------------------- .../plugins/__snapshots__/plugin.spec.ts.snap | 9 ++++++ packages/jest/src/plugins/plugin.spec.ts | 21 ++++++++++++ packages/jest/src/plugins/plugin.ts | 4 +++ 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/packages/jest/src/generators/configuration/configuration.ts b/packages/jest/src/generators/configuration/configuration.ts index a0894bce8ccf5..e448819cee423 100644 --- a/packages/jest/src/generators/configuration/configuration.ts +++ b/packages/jest/src/generators/configuration/configuration.ts @@ -123,39 +123,7 @@ export async function configurationGeneratorInternal( await formatFiles(tree); } - tasks.push(getUnsupportedModuleResolutionWarningTask(tree)); - return runTasksInSerial(...tasks); } -/** - * For Jest < 30, there is no way to load jest.config.ts file if the tsconfig.json/tsconfig.base.json sets moduleResolution to bundler or nodenext. - * Jest uses ts-node in a way that is not compatible, so until this is fixed we need to log a warning. - * See: https://github.com/jestjs/jest/blob/main/packages/jest-config/src/readConfigFileAndSetRootDir.ts#L145-L153 - */ -function getUnsupportedModuleResolutionWarningTask( - tree: Tree -): GeneratorCallback { - const tsConfigFileName = getRootTsConfigFileName(tree); - if (tsConfigFileName) { - const json = readJson(tree, tsConfigFileName); - if ( - json.compilerOptions.moduleResolution !== 'node' && - json.compilerOptions.moduleResolution !== 'node10' - ) { - return () => { - output.warn({ - title: `Compiler option 'moduleResolution' in ${tsConfigFileName} must be 'node' or 'node10'`, - bodyLines: [ - `Jest requires 'moduleResolution' to be set to 'node' or 'node10' to work properly. It would need to be changed in the "${tsConfigFileName}" file. It's not enough to override the compiler option in the project's tsconfig file.`, - `Alternatively, you can use the environment variable \`TS_NODE_COMPILER_OPTIONS='{"moduleResolution": "node10"}'\` to override Jest's usage of ts-node.`, - ], - }); - }; - } - } - - return () => {}; -} - export default configurationGenerator; diff --git a/packages/jest/src/plugins/__snapshots__/plugin.spec.ts.snap b/packages/jest/src/plugins/__snapshots__/plugin.spec.ts.snap index 31745e4220588..5c61a128b4c03 100644 --- a/packages/jest/src/plugins/__snapshots__/plugin.spec.ts.snap +++ b/packages/jest/src/plugins/__snapshots__/plugin.spec.ts.snap @@ -39,6 +39,9 @@ exports[`@nx/jest/plugin should add package as externalDependencies to the input }, "options": { "cwd": "proj", + "env": { + "TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}", + }, }, "outputs": [ "{workspaceRoot}/coverage", @@ -91,6 +94,9 @@ exports[`@nx/jest/plugin should add package as externalDependencies to the input }, "options": { "cwd": "proj", + "env": { + "TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}", + }, }, "outputs": [ "{workspaceRoot}/coverage", @@ -143,6 +149,9 @@ exports[`@nx/jest/plugin should add package as externalDependencies to the input }, "options": { "cwd": "proj", + "env": { + "TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}", + }, }, "outputs": [ "{workspaceRoot}/coverage", diff --git a/packages/jest/src/plugins/plugin.spec.ts b/packages/jest/src/plugins/plugin.spec.ts index db646d1feb4d5..3cf4ae505883f 100644 --- a/packages/jest/src/plugins/plugin.spec.ts +++ b/packages/jest/src/plugins/plugin.spec.ts @@ -96,6 +96,9 @@ describe('@nx/jest/plugin', () => { }, "options": { "cwd": "proj", + "env": { + "TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}", + }, }, "outputs": [ "{workspaceRoot}/coverage", @@ -173,6 +176,9 @@ describe('@nx/jest/plugin', () => { }, "options": { "cwd": "proj", + "env": { + "TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}", + }, }, "outputs": [ "{workspaceRoot}/coverage", @@ -306,6 +312,9 @@ describe('@nx/jest/plugin', () => { }, "options": { "cwd": "proj", + "env": { + "TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}", + }, }, "outputs": [ "{workspaceRoot}/coverage", @@ -413,6 +422,9 @@ describe('@nx/jest/plugin', () => { }, "options": { "cwd": "proj", + "env": { + "TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}", + }, }, "outputs": [ "{workspaceRoot}/coverage", @@ -480,6 +492,9 @@ describe('@nx/jest/plugin', () => { }, "options": { "cwd": "proj", + "env": { + "TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}", + }, }, "outputs": [ "{workspaceRoot}/coverage", @@ -593,6 +608,9 @@ describe('@nx/jest/plugin', () => { }, "options": { "cwd": "proj", + "env": { + "TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}", + }, }, "outputs": [ "{workspaceRoot}/coverage", @@ -660,6 +678,9 @@ describe('@nx/jest/plugin', () => { }, "options": { "cwd": "proj", + "env": { + "TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}", + }, }, "outputs": [ "{workspaceRoot}/coverage", diff --git a/packages/jest/src/plugins/plugin.ts b/packages/jest/src/plugins/plugin.ts index d143489a166ee..b761c25233203 100644 --- a/packages/jest/src/plugins/plugin.ts +++ b/packages/jest/src/plugins/plugin.ts @@ -192,6 +192,9 @@ async function buildJestTargets( command: 'jest', options: { cwd: projectRoot, + // Jest registers ts-node with module CJS https://github.com/SimenB/jest/blob/v29.6.4/packages/jest-config/src/readConfigFileAndSetRootDir.ts#L117-L119 + // We want to support of ESM via 'module':'nodenext', we need to override the resolution until Jest supports it. + env: { TS_NODE_COMPILER_OPTIONS: '{"moduleResolution":"node10"}' }, }, metadata: { technologies: ['jest'], @@ -269,6 +272,7 @@ async function buildJestTargets( outputs, options: { cwd: projectRoot, + env: { TS_NODE_COMPILER_OPTIONS: '{"moduleResolution":"node10"}' }, }, metadata: { technologies: ['jest'],