Repository: facebook/jest
File: e2e/__tests__/jest.config.ts.test.ts (TypeScript)

Code before:
    path.join(DIR, 'some', 'nested', 'directory'),
    ['-w=1', '--ci=false'],
    {nodeOptions: '--no-warnings', skipPkgJsonCheck: true},
  );

  // Snapshot the console.logged `process.cwd()` and make sure it stays the same
  expect(stdout.replaceAll(/^\W+(.*)e2e/gm, '<<REPLACED>>')).toMatchSnapshot();

  const {rest, summary} = extractSummary(stderr);
  expect(exitCode).toBe(0);
  expect(rest).toMatchSnapshot();
  expect(summary).toMatchSnapshot();
});

onNodeVersions('<22.6', () => {
  const jestPath = require.resolve('jest');
  const jestTypesPath = jestPath.replace(/\.js$/, '.d.ts');
  const jestTypesExists = fs.existsSync(jestTypesPath);

  (jestTypesExists ? test : test.skip).each([true, false])(
    'check the config disabled (skip type check: %p)',
    skipTypeCheck => {
      writeFiles(DIR, {
        '__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
        'jest.config.ts': `
        /**@jest-config-loader-options {"transpileOnly":${!!skipTypeCheck}}*/
        import {Config} from 'jest';
        const config: Config = { testTimeout: "10000" };
        export default config;
      `,
        'package.json': '{}',
      });

      const typeErrorString =
        "TS2322: Type 'string' is not assignable to type 'number'.";
      const runtimeErrorString = 'Option "testTimeout" must be of type:';

      const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']);

      if (skipTypeCheck) {
        expect(stderr).not.toMatch(typeErrorString);
        expect(stderr).toMatch(runtimeErrorString);
      } else {
        expect(stderr).toMatch(typeErrorString);
        expect(stderr).not.toMatch(runtimeErrorString);
      }

      expect(exitCode).toBe(1);
    },
  );


Review comment (suggestion):
Skipping this test in new node versions since the node feature only transpiles and doesn't type check.

Code after:
    path.join(DIR, 'some', 'nested', 'directory'),
    ['-w=1', '--ci=false'],
    {nodeOptions: '--no-warnings', skipPkgJsonCheck: true},
  );

  // Snapshot the console.logged `process.cwd()` and make sure it stays the same
  expect(
    stdout
      .replaceAll(/^\W+(.*)e2e/gm, '<<REPLACED>>')
      // slightly different log in node versions >= 23
      .replace('at Object.log', 'at Object.<anonymous>'),
  ).toMatchSnapshot();

  const {rest, summary} = extractSummary(stderr);
  expect(exitCode).toBe(0);
  expect(rest).toMatchSnapshot();
  expect(summary).toMatchSnapshot();
});

onNodeVersions('<23.6', () => {
  const jestPath = require.resolve('jest');
  const jestTypesPath = jestPath.replace(/\.js$/, '.d.ts');
  const jestTypesExists = fs.existsSync(jestTypesPath);

  (jestTypesExists ? test : test.skip).each([true, false])(
    'check the config disabled (skip type check: %p)',
    skipTypeCheck => {
      writeFiles(DIR, {
        '__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
        'jest.config.ts': `
        /**@jest-config-loader-options {"transpileOnly":${!!skipTypeCheck}}*/
        import {Config} from 'jest';
        const config: Config = { testTimeout: "10000" };
        export default config;
      `,
        'package.json': '{}',
      });

      const typeErrorString =
        "TS2322: Type 'string' is not assignable to type 'number'.";
      const runtimeErrorString = 'Option "testTimeout" must be of type:';

      const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']);

      if (skipTypeCheck) {
        expect(stderr).not.toMatch(typeErrorString);
        expect(stderr).toMatch(runtimeErrorString);
      } else {
        expect(stderr).toMatch(typeErrorString);
        expect(stderr).not.toMatch(runtimeErrorString);
      }