Skip to content

Commit 0eab327

Browse files
authored
Fix for the jest.config.ts compiler to not interfere with tsconfig.json files (#10675)
1 parent 69ae5b5 commit 0eab327

File tree

6 files changed

+38
-5
lines changed

6 files changed

+38
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Fixes
88

99
- `[expect]` Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508))
10+
- `[jest-config]` Fix for the `jest.config.ts` compiler to not interfere with `tsconfig.json` files ([#10675](https://github.com/facebook/jest/pull/10675))
1011
- `[jest-message-util]` Update to work properly with Node 15 ([#10660](https://github.com/facebook/jest/pull/10660))
1112
- `[jest-mock]` Allow to mock methods in getters (TypeScript 3.9 export) ([#10156](https://github.com/facebook/jest/pull/10156))
1213

e2e/__tests__/__snapshots__/jest.config.js.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`traverses directory tree up until it finds jest.config 1`] = `
44
console.log
5-
<<REPLACED>>/jest.config.js/some/nested/directory
5+
<<REPLACED>>/jest-config-js/some/nested/directory
66
77
at Object.log (__tests__/a-banana.js:3:27)
88

e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`traverses directory tree up until it finds jest.config 1`] = `
44
console.log
5-
<<REPLACED>>/jest.config.ts/some/nested/directory
5+
<<REPLACED>>/jest-config-ts/some/nested/directory
66
77
at Object.log (__tests__/a-giraffe.js:3:27)
88
@@ -34,3 +34,16 @@ Snapshots: 0 total
3434
Time: <<REPLACED>>
3535
Ran all test suites.
3636
`;
37+
38+
exports[`works with tsconfig.json 1`] = `
39+
PASS __tests__/a-giraffe.js
40+
✓ giraffe
41+
`;
42+
43+
exports[`works with tsconfig.json 2`] = `
44+
Test Suites: 1 passed, 1 total
45+
Tests: 1 passed, 1 total
46+
Snapshots: 0 total
47+
Time: <<REPLACED>>
48+
Ran all test suites.
49+
`;

e2e/__tests__/jest.config.js.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {wrap} from 'jest-snapshot-serializer-raw';
1010
import runJest from '../runJest';
1111
import {cleanup, extractSummary, writeFiles} from '../Utils';
1212

13-
const DIR = path.resolve(__dirname, '../jest.config.js');
13+
const DIR = path.resolve(__dirname, '../jest-config-js');
1414

1515
beforeEach(() => cleanup(DIR));
1616
afterAll(() => cleanup(DIR));

e2e/__tests__/jest.config.ts.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {wrap} from 'jest-snapshot-serializer-raw';
1010
import runJest from '../runJest';
1111
import {cleanup, extractSummary, writeFiles} from '../Utils';
1212

13-
const DIR = path.resolve(__dirname, '../jest.config.ts');
13+
const DIR = path.resolve(__dirname, '../jest-config-ts');
1414

1515
beforeEach(() => cleanup(DIR));
1616
afterAll(() => cleanup(DIR));
@@ -29,6 +29,21 @@ test('works with jest.config.ts', () => {
2929
expect(wrap(summary)).toMatchSnapshot();
3030
});
3131

32+
test('works with tsconfig.json', () => {
33+
writeFiles(DIR, {
34+
'__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`,
35+
'jest.config.ts': `export default {testEnvironment: 'jest-environment-node', testRegex: '.*-giraffe.js'};`,
36+
'package.json': '{}',
37+
'tsconfig.json': '{ "compilerOptions": { "module": "esnext" } }',
38+
});
39+
40+
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']);
41+
const {rest, summary} = extractSummary(stderr);
42+
expect(exitCode).toBe(0);
43+
expect(wrap(rest)).toMatchSnapshot();
44+
expect(wrap(summary)).toMatchSnapshot();
45+
});
46+
3247
test('traverses directory tree up until it finds jest.config', () => {
3348
writeFiles(DIR, {
3449
'__tests__/a-giraffe.js': `

packages/jest-config/src/readConfigFileAndSetRootDir.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ const loadTSConfigFile = async (
107107

108108
// Register TypeScript compiler instance
109109
try {
110-
registerer = require('ts-node').register();
110+
registerer = require('ts-node').register({
111+
compilerOptions: {
112+
module: 'CommonJS',
113+
},
114+
});
111115
} catch (e) {
112116
if (e.code === 'MODULE_NOT_FOUND') {
113117
throw new Error(

0 commit comments

Comments
 (0)