Skip to content

Commit 3569dd0

Browse files
authored
fix(tests): Fix test failures due to unflagged strip-types (#15795)
1 parent fc4d295 commit 3569dd0

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

e2e/__tests__/tsIntegration.test.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77

88
import * as path from 'path';
99
import {cleanup, writeFiles} from '../Utils';
10-
import runJest, {getConfig} from '../runJest';
10+
import runJest, {getConfig, useNativeTypeScript} from '../runJest';
1111

1212
const DIR = path.resolve(__dirname, '../ts-node-integration');
1313

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

17+
// Node.js's builtin TypeScript support doesn't do type checking, so skip tests
18+
// that validate type-checking behavior.
19+
const testIfTsLoader = useNativeTypeScript ? test.skip : test;
20+
1721
describe('when `Config` type is imported from "@jest/types"', () => {
1822
test('with object config exported from TS file', () => {
1923
writeFiles(DIR, {
@@ -93,7 +97,7 @@ describe('when `Config` type is imported from "@jest/types"', () => {
9397
expect(globalConfig.verbose).toBe(true);
9498
});
9599

96-
test('throws if type errors are encountered', () => {
100+
testIfTsLoader('throws if type errors are encountered', () => {
97101
writeFiles(DIR, {
98102
'__tests__/dummy.test.js': "test('dummy', () => expect(123).toBe(123));",
99103
'jest.config.ts': `
@@ -247,24 +251,27 @@ describe('when `Config` type is imported from "@jest/types"', () => {
247251
expect(globalConfig.verbose).toBe(true);
248252
});
249253

250-
test('throws if type errors are encountered when package.json#type=module', () => {
251-
writeFiles(DIR, {
252-
'__tests__/dummy.test.js': "test('dummy', () => expect(12).toBe(12));",
253-
'jest.config.ts': `
254+
testIfTsLoader(
255+
'throws if type errors are encountered when package.json#type=module',
256+
() => {
257+
writeFiles(DIR, {
258+
'__tests__/dummy.test.js': "test('dummy', () => expect(12).toBe(12));",
259+
'jest.config.ts': `
254260
import type {Config} from '@jest/types';
255261
const config: Config.InitialOptions = {testTimeout: '10000'};
256262
export default config;
257263
`,
258-
'package.json': '{"type": "module"}',
259-
});
264+
'package.json': '{"type": "module"}',
265+
});
260266

261-
const {stderr, exitCode} = runJest(DIR);
267+
const {stderr, exitCode} = runJest(DIR);
262268

263-
expect(stderr).toMatch(
264-
"jest.config.ts(2,40): error TS2322: Type 'string' is not assignable to type 'number'.",
265-
);
266-
expect(exitCode).toBe(1);
267-
});
269+
expect(stderr).toMatch(
270+
"jest.config.ts(2,40): error TS2322: Type 'string' is not assignable to type 'number'.",
271+
);
272+
expect(exitCode).toBe(1);
273+
},
274+
);
268275

269276
test('throws if syntax errors are encountered when package.json#type=module', () => {
270277
writeFiles(DIR, {
@@ -403,7 +410,7 @@ describe('when `Config` type is imported from "jest"', () => {
403410
expect(globalConfig.verbose).toBe(true);
404411
});
405412

406-
test('throws if type errors are encountered', () => {
413+
testIfTsLoader('throws if type errors are encountered', () => {
407414
writeFiles(DIR, {
408415
'__tests__/dummy.test.js': "test('dummy', () => expect(123).toBe(123));",
409416
'jest.config.ts': `
@@ -557,24 +564,27 @@ describe('when `Config` type is imported from "jest"', () => {
557564
expect(globalConfig.verbose).toBe(true);
558565
});
559566

560-
test('throws if type errors are encountered when package.json#type=module', () => {
561-
writeFiles(DIR, {
562-
'__tests__/dummy.test.js': "test('dummy', () => expect(12).toBe(12));",
563-
'jest.config.ts': `
567+
testIfTsLoader(
568+
'throws if type errors are encountered when package.json#type=module',
569+
() => {
570+
writeFiles(DIR, {
571+
'__tests__/dummy.test.js': "test('dummy', () => expect(12).toBe(12));",
572+
'jest.config.ts': `
564573
import type {Config} from 'jest';
565574
const config: Config = {testTimeout: '10000'};
566575
export default config;
567576
`,
568-
'package.json': '{"type": "module"}',
569-
});
577+
'package.json': '{"type": "module"}',
578+
});
570579

571-
const {stderr, exitCode} = runJest(DIR);
580+
const {stderr, exitCode} = runJest(DIR);
572581

573-
expect(stderr).toMatch(
574-
"jest.config.ts(2,25): error TS2322: Type 'string' is not assignable to type 'number'.",
575-
);
576-
expect(exitCode).toBe(1);
577-
});
582+
expect(stderr).toMatch(
583+
"jest.config.ts(2,25): error TS2322: Type 'string' is not assignable to type 'number'.",
584+
);
585+
expect(exitCode).toBe(1);
586+
},
587+
);
578588

579589
test('throws if syntax errors are encountered when package.json#type=module', () => {
580590
writeFiles(DIR, {

e2e/__tests__/typescriptConfigFile.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88
import {tmpdir} from 'os';
99
import * as path from 'path';
10-
import * as semver from 'semver';
1110
import {onNodeVersions} from '@jest/test-utils';
1211
import {cleanup, writeFiles} from '../Utils';
13-
import runJest, {getConfig} from '../runJest';
12+
import runJest, {getConfig, useNativeTypeScript} from '../runJest';
1413

1514
const DIR = path.resolve(tmpdir(), 'typescript-config-file');
16-
const useNativeTypeScript = semver.satisfies(process.versions.node, '>=23.6.0');
1715
const importFileExtension = useNativeTypeScript ? '.ts' : '';
1816

1917
beforeEach(() => cleanup(DIR));

e2e/runJest.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ import {stripVTControlCharacters as stripAnsi} from 'util';
1212
import dedent from 'dedent';
1313
import execa from 'execa';
1414
import * as fs from 'graceful-fs';
15+
import * as semver from 'semver';
1516
import {TestPathPatterns} from '@jest/pattern';
1617
import type {FormattedTestResults} from '@jest/test-result';
1718
import {normalizeIcons} from '@jest/test-utils';
1819
import type {Config} from '@jest/types';
1920
import {ErrorWithStack} from 'jest-util';
2021

22+
export const useNativeTypeScript = semver.satisfies(
23+
process.versions.node,
24+
'^22.18.0 || >=23.6.0',
25+
);
26+
2127
const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js');
2228

2329
type RunJestOptions = {

0 commit comments

Comments
 (0)