Skip to content

Commit 6afc286

Browse files
authored
fix(core): jest plugin should work with nodenext set in tsconfig (#28820)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> When you create a new project using the new ts setup to support ts project references jest does not work out of the box due to incompatibility with the module being set inside tsconfig to `nodenext`. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> Jest should work out of the box with our current setup ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent a22a431 commit 6afc286

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

packages/jest/src/generators/configuration/configuration.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -123,39 +123,7 @@ export async function configurationGeneratorInternal(
123123
await formatFiles(tree);
124124
}
125125

126-
tasks.push(getUnsupportedModuleResolutionWarningTask(tree));
127-
128126
return runTasksInSerial(...tasks);
129127
}
130128

131-
/**
132-
* 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.
133-
* Jest uses ts-node in a way that is not compatible, so until this is fixed we need to log a warning.
134-
* See: https://github.com/jestjs/jest/blob/main/packages/jest-config/src/readConfigFileAndSetRootDir.ts#L145-L153
135-
*/
136-
function getUnsupportedModuleResolutionWarningTask(
137-
tree: Tree
138-
): GeneratorCallback {
139-
const tsConfigFileName = getRootTsConfigFileName(tree);
140-
if (tsConfigFileName) {
141-
const json = readJson(tree, tsConfigFileName);
142-
if (
143-
json.compilerOptions.moduleResolution !== 'node' &&
144-
json.compilerOptions.moduleResolution !== 'node10'
145-
) {
146-
return () => {
147-
output.warn({
148-
title: `Compiler option 'moduleResolution' in ${tsConfigFileName} must be 'node' or 'node10'`,
149-
bodyLines: [
150-
`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.`,
151-
`Alternatively, you can use the environment variable \`TS_NODE_COMPILER_OPTIONS='{"moduleResolution": "node10"}'\` to override Jest's usage of ts-node.`,
152-
],
153-
});
154-
};
155-
}
156-
}
157-
158-
return () => {};
159-
}
160-
161129
export default configurationGenerator;

packages/jest/src/plugins/__snapshots__/plugin.spec.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ exports[`@nx/jest/plugin should add package as externalDependencies to the input
3939
},
4040
"options": {
4141
"cwd": "proj",
42+
"env": {
43+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
44+
},
4245
},
4346
"outputs": [
4447
"{workspaceRoot}/coverage",
@@ -91,6 +94,9 @@ exports[`@nx/jest/plugin should add package as externalDependencies to the input
9194
},
9295
"options": {
9396
"cwd": "proj",
97+
"env": {
98+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
99+
},
94100
},
95101
"outputs": [
96102
"{workspaceRoot}/coverage",
@@ -143,6 +149,9 @@ exports[`@nx/jest/plugin should add package as externalDependencies to the input
143149
},
144150
"options": {
145151
"cwd": "proj",
152+
"env": {
153+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
154+
},
146155
},
147156
"outputs": [
148157
"{workspaceRoot}/coverage",

packages/jest/src/plugins/plugin.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ describe('@nx/jest/plugin', () => {
9696
},
9797
"options": {
9898
"cwd": "proj",
99+
"env": {
100+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
101+
},
99102
},
100103
"outputs": [
101104
"{workspaceRoot}/coverage",
@@ -173,6 +176,9 @@ describe('@nx/jest/plugin', () => {
173176
},
174177
"options": {
175178
"cwd": "proj",
179+
"env": {
180+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
181+
},
176182
},
177183
"outputs": [
178184
"{workspaceRoot}/coverage",
@@ -306,6 +312,9 @@ describe('@nx/jest/plugin', () => {
306312
},
307313
"options": {
308314
"cwd": "proj",
315+
"env": {
316+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
317+
},
309318
},
310319
"outputs": [
311320
"{workspaceRoot}/coverage",
@@ -413,6 +422,9 @@ describe('@nx/jest/plugin', () => {
413422
},
414423
"options": {
415424
"cwd": "proj",
425+
"env": {
426+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
427+
},
416428
},
417429
"outputs": [
418430
"{workspaceRoot}/coverage",
@@ -480,6 +492,9 @@ describe('@nx/jest/plugin', () => {
480492
},
481493
"options": {
482494
"cwd": "proj",
495+
"env": {
496+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
497+
},
483498
},
484499
"outputs": [
485500
"{workspaceRoot}/coverage",
@@ -593,6 +608,9 @@ describe('@nx/jest/plugin', () => {
593608
},
594609
"options": {
595610
"cwd": "proj",
611+
"env": {
612+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
613+
},
596614
},
597615
"outputs": [
598616
"{workspaceRoot}/coverage",
@@ -660,6 +678,9 @@ describe('@nx/jest/plugin', () => {
660678
},
661679
"options": {
662680
"cwd": "proj",
681+
"env": {
682+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
683+
},
663684
},
664685
"outputs": [
665686
"{workspaceRoot}/coverage",

packages/jest/src/plugins/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ async function buildJestTargets(
192192
command: 'jest',
193193
options: {
194194
cwd: projectRoot,
195+
// Jest registers ts-node with module CJS https://github.com/SimenB/jest/blob/v29.6.4/packages/jest-config/src/readConfigFileAndSetRootDir.ts#L117-L119
196+
// We want to support of ESM via 'module':'nodenext', we need to override the resolution until Jest supports it.
197+
env: { TS_NODE_COMPILER_OPTIONS: '{"moduleResolution":"node10"}' },
195198
},
196199
metadata: {
197200
technologies: ['jest'],
@@ -269,6 +272,7 @@ async function buildJestTargets(
269272
outputs,
270273
options: {
271274
cwd: projectRoot,
275+
env: { TS_NODE_COMPILER_OPTIONS: '{"moduleResolution":"node10"}' },
272276
},
273277
metadata: {
274278
technologies: ['jest'],

0 commit comments

Comments
 (0)