From 0eab3273e704e6bf34dbd355d87faf40dc0fdb27 Mon Sep 17 00:00:00 2001 From: Gamote <38785171+Gamote@users.noreply.github.com> Date: Thu, 22 Oct 2020 11:32:22 +0200 Subject: [PATCH] Fix for the `jest.config.ts` compiler to not interfere with `tsconfig.json` files (#10675) --- CHANGELOG.md | 1 + .../__snapshots__/jest.config.js.test.ts.snap | 2 +- .../__snapshots__/jest.config.ts.test.ts.snap | 15 ++++++++++++++- e2e/__tests__/jest.config.js.test.ts | 2 +- e2e/__tests__/jest.config.ts.test.ts | 17 ++++++++++++++++- .../src/readConfigFileAndSetRootDir.ts | 6 +++++- 6 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eaee24dc0baa..41669d689333 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Fixes - `[expect]` Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508)) +- `[jest-config]` Fix for the `jest.config.ts` compiler to not interfere with `tsconfig.json` files ([#10675](https://github.com/facebook/jest/pull/10675)) - `[jest-message-util]` Update to work properly with Node 15 ([#10660](https://github.com/facebook/jest/pull/10660)) - `[jest-mock]` Allow to mock methods in getters (TypeScript 3.9 export) ([#10156](https://github.com/facebook/jest/pull/10156)) diff --git a/e2e/__tests__/__snapshots__/jest.config.js.test.ts.snap b/e2e/__tests__/__snapshots__/jest.config.js.test.ts.snap index db94a91b08e4..9bf15c021352 100644 --- a/e2e/__tests__/__snapshots__/jest.config.js.test.ts.snap +++ b/e2e/__tests__/__snapshots__/jest.config.js.test.ts.snap @@ -2,7 +2,7 @@ exports[`traverses directory tree up until it finds jest.config 1`] = ` console.log -<>/jest.config.js/some/nested/directory +<>/jest-config-js/some/nested/directory at Object.log (__tests__/a-banana.js:3:27) diff --git a/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap b/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap index 231d926a36de..5930200701cb 100644 --- a/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap +++ b/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap @@ -2,7 +2,7 @@ exports[`traverses directory tree up until it finds jest.config 1`] = ` console.log -<>/jest.config.ts/some/nested/directory +<>/jest-config-ts/some/nested/directory at Object.log (__tests__/a-giraffe.js:3:27) @@ -34,3 +34,16 @@ Snapshots: 0 total Time: <> Ran all test suites. `; + +exports[`works with tsconfig.json 1`] = ` +PASS __tests__/a-giraffe.js + ✓ giraffe +`; + +exports[`works with tsconfig.json 2`] = ` +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +`; diff --git a/e2e/__tests__/jest.config.js.test.ts b/e2e/__tests__/jest.config.js.test.ts index 5fd47a0ba1c9..a81611071cd7 100644 --- a/e2e/__tests__/jest.config.js.test.ts +++ b/e2e/__tests__/jest.config.js.test.ts @@ -10,7 +10,7 @@ import {wrap} from 'jest-snapshot-serializer-raw'; import runJest from '../runJest'; import {cleanup, extractSummary, writeFiles} from '../Utils'; -const DIR = path.resolve(__dirname, '../jest.config.js'); +const DIR = path.resolve(__dirname, '../jest-config-js'); beforeEach(() => cleanup(DIR)); afterAll(() => cleanup(DIR)); diff --git a/e2e/__tests__/jest.config.ts.test.ts b/e2e/__tests__/jest.config.ts.test.ts index 9b3edcef87ff..d93dc33bfd84 100644 --- a/e2e/__tests__/jest.config.ts.test.ts +++ b/e2e/__tests__/jest.config.ts.test.ts @@ -10,7 +10,7 @@ import {wrap} from 'jest-snapshot-serializer-raw'; import runJest from '../runJest'; import {cleanup, extractSummary, writeFiles} from '../Utils'; -const DIR = path.resolve(__dirname, '../jest.config.ts'); +const DIR = path.resolve(__dirname, '../jest-config-ts'); beforeEach(() => cleanup(DIR)); afterAll(() => cleanup(DIR)); @@ -29,6 +29,21 @@ test('works with jest.config.ts', () => { expect(wrap(summary)).toMatchSnapshot(); }); +test('works with tsconfig.json', () => { + writeFiles(DIR, { + '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, + 'jest.config.ts': `export default {testEnvironment: 'jest-environment-node', testRegex: '.*-giraffe.js'};`, + 'package.json': '{}', + 'tsconfig.json': '{ "compilerOptions": { "module": "esnext" } }', + }); + + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); + const {rest, summary} = extractSummary(stderr); + expect(exitCode).toBe(0); + expect(wrap(rest)).toMatchSnapshot(); + expect(wrap(summary)).toMatchSnapshot(); +}); + test('traverses directory tree up until it finds jest.config', () => { writeFiles(DIR, { '__tests__/a-giraffe.js': ` diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 47f1d6823efc..68cee0e87a6b 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -107,7 +107,11 @@ const loadTSConfigFile = async ( // Register TypeScript compiler instance try { - registerer = require('ts-node').register(); + registerer = require('ts-node').register({ + compilerOptions: { + module: 'CommonJS', + }, + }); } catch (e) { if (e.code === 'MODULE_NOT_FOUND') { throw new Error(