Skip to content

Commit a3975c8

Browse files
authored
fix: allow Node16/NodeNext/Bundler moduleResolution in project's tsconfig (#14739)
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.
1 parent 18cde1d commit a3975c8

File tree

7 files changed

+52
-1
lines changed

7 files changed

+52
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
- `[jest-config]` Support `testTimeout` in project config ([#14697](https://github.com/jestjs/jest/pull/14697))
5353
- `[jest-config]` Support `coverageReporters` in project config ([#14697](https://github.com/jestjs/jest/pull/14830))
5454
- `[jest-config]` Allow `reporters` in project config ([#14768](https://github.com/jestjs/jest/pull/14768))
55+
- `[jest-config]` Allow Node16/NodeNext/Bundler `moduleResolution` in project's tsconfig ([#14739](https://github.com/jestjs/jest/pull/14739))
5556
- `[jest-each]` Allow `$keypath` templates with `null` or `undefined` values ([#14831](https://github.com/jestjs/jest/pull/14831))
5657
- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408))
5758
- `[@jest/expect-utils]` [**BREAKING**] exclude non-enumerable in object matching ([#14670](https://github.com/jestjs/jest/pull/14670))

e2e/__tests__/typescriptConfigFile.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import {tmpdir} from 'os';
99
import * as path from 'path';
1010
import {cleanup, writeFiles} from '../Utils';
11-
import runJest from '../runJest';
11+
import runJest, {getConfig} from '../runJest';
1212

1313
const DIR = path.resolve(tmpdir(), 'typescript-config-file');
1414

@@ -107,3 +107,19 @@ test('works with multiple typescript configs that import something', () => {
107107
expect(exitCode).toBe(0);
108108
expect(stdout).toBe('');
109109
});
110+
111+
test("works with single typescript config that does not import anything with project's moduleResolution set to Node16", () => {
112+
const {configs} = getConfig(
113+
'typescript-config/modern-module-resolution',
114+
[],
115+
{
116+
skipPkgJsonCheck: true,
117+
},
118+
);
119+
120+
expect(configs).toHaveLength(1);
121+
expect(configs[0].displayName).toEqual({
122+
color: 'white',
123+
name: 'Config from modern ts file',
124+
});
125+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
test('dummy test', () => {
9+
expect(1).toBe(1);
10+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
const config = {
9+
displayName: 'Config from modern ts file',
10+
testEnvironment: 'node',
11+
};
12+
export default config;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "Node16",
5+
"moduleResolution": "Node16",
6+
"strict": true
7+
}
8+
}

packages/jest-config/src/readConfigFileAndSetRootDir.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ async function registerTsNode(): Promise<Service> {
119119
return tsNode.register({
120120
compilerOptions: {
121121
module: 'CommonJS',
122+
moduleResolution: 'Node10',
122123
},
123124
moduleTypes: {
124125
'**': 'cjs',

0 commit comments

Comments
 (0)