Skip to content

Commit

Permalink
fix(core): projects should work with nodenext set in tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Nov 6, 2024
1 parent b14d5c5 commit 7c462f5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
32 changes: 0 additions & 32 deletions packages/jest/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 9 additions & 0 deletions packages/jest/src/plugins/__snapshots__/plugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
21 changes: 21 additions & 0 deletions packages/jest/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ describe('@nx/jest/plugin', () => {
},
"options": {
"cwd": "proj",
"env": {
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
},
},
"outputs": [
"{workspaceRoot}/coverage",
Expand Down Expand Up @@ -173,6 +176,9 @@ describe('@nx/jest/plugin', () => {
},
"options": {
"cwd": "proj",
"env": {
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
},
},
"outputs": [
"{workspaceRoot}/coverage",
Expand Down Expand Up @@ -306,6 +312,9 @@ describe('@nx/jest/plugin', () => {
},
"options": {
"cwd": "proj",
"env": {
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
},
},
"outputs": [
"{workspaceRoot}/coverage",
Expand Down Expand Up @@ -413,6 +422,9 @@ describe('@nx/jest/plugin', () => {
},
"options": {
"cwd": "proj",
"env": {
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
},
},
"outputs": [
"{workspaceRoot}/coverage",
Expand Down Expand Up @@ -480,6 +492,9 @@ describe('@nx/jest/plugin', () => {
},
"options": {
"cwd": "proj",
"env": {
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
},
},
"outputs": [
"{workspaceRoot}/coverage",
Expand Down Expand Up @@ -593,6 +608,9 @@ describe('@nx/jest/plugin', () => {
},
"options": {
"cwd": "proj",
"env": {
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
},
},
"outputs": [
"{workspaceRoot}/coverage",
Expand Down Expand Up @@ -660,6 +678,9 @@ describe('@nx/jest/plugin', () => {
},
"options": {
"cwd": "proj",
"env": {
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"node10"}",
},
},
"outputs": [
"{workspaceRoot}/coverage",
Expand Down
4 changes: 4 additions & 0 deletions packages/jest/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -269,6 +272,7 @@ async function buildJestTargets(
outputs,
options: {
cwd: projectRoot,
env: { TS_NODE_COMPILER_OPTIONS: '{"moduleResolution":"node10"}' },
},
metadata: {
technologies: ['jest'],
Expand Down

0 comments on commit 7c462f5

Please sign in to comment.