Skip to content

Commit

Permalink
fix: allow Node16/NodeNext/Bundler moduleResolution in project's tsco…
Browse files Browse the repository at this point in the history
…nfig (#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.
  • Loading branch information
akwodkiewicz authored May 21, 2024
1 parent 18cde1d commit a3975c8
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- `[jest-config]` Support `testTimeout` in project config ([#14697](https://github.com/jestjs/jest/pull/14697))
- `[jest-config]` Support `coverageReporters` in project config ([#14697](https://github.com/jestjs/jest/pull/14830))
- `[jest-config]` Allow `reporters` in project config ([#14768](https://github.com/jestjs/jest/pull/14768))
- `[jest-config]` Allow Node16/NodeNext/Bundler `moduleResolution` in project's tsconfig ([#14739](https://github.com/jestjs/jest/pull/14739))
- `[jest-each]` Allow `$keypath` templates with `null` or `undefined` values ([#14831](https://github.com/jestjs/jest/pull/14831))
- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408))
- `[@jest/expect-utils]` [**BREAKING**] exclude non-enumerable in object matching ([#14670](https://github.com/jestjs/jest/pull/14670))
Expand Down
18 changes: 17 additions & 1 deletion e2e/__tests__/typescriptConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import {tmpdir} from 'os';
import * as path from 'path';
import {cleanup, writeFiles} from '../Utils';
import runJest from '../runJest';
import runJest, {getConfig} from '../runJest';

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

Expand Down Expand Up @@ -107,3 +107,19 @@ test('works with multiple typescript configs that import something', () => {
expect(exitCode).toBe(0);
expect(stdout).toBe('');
});

test("works with single typescript config that does not import anything with project's moduleResolution set to Node16", () => {
const {configs} = getConfig(
'typescript-config/modern-module-resolution',
[],
{
skipPkgJsonCheck: true,
},
);

expect(configs).toHaveLength(1);
expect(configs[0].displayName).toEqual({
color: 'white',
name: 'Config from modern ts file',
});
});
10 changes: 10 additions & 0 deletions e2e/typescript-config/modern-module-resolution/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('dummy test', () => {
expect(1).toBe(1);
});
12 changes: 12 additions & 0 deletions e2e/typescript-config/modern-module-resolution/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const config = {
displayName: 'Config from modern ts file',
testEnvironment: 'node',
};
export default config;
3 changes: 3 additions & 0 deletions e2e/typescript-config/modern-module-resolution/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
8 changes: 8 additions & 0 deletions e2e/typescript-config/modern-module-resolution/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "Node16",
"moduleResolution": "Node16",
"strict": true
}
}
1 change: 1 addition & 0 deletions packages/jest-config/src/readConfigFileAndSetRootDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ async function registerTsNode(): Promise<Service> {
return tsNode.register({
compilerOptions: {
module: 'CommonJS',
moduleResolution: 'Node10',
},
moduleTypes: {
'**': 'cjs',
Expand Down

0 comments on commit a3975c8

Please sign in to comment.