Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): jest plugin should work with nodenext set in tsconfig #28820

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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