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

Code before:
/**
 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
import path = require('path');
import execa = require('execa');
import {readInitialOptions} from 'jest-config';

function resolveFixture(...pathSegments: Array<string>) {
  return path.resolve(__dirname, '..', 'read-initial-options', ...pathSegments);
}

describe(readInitialOptions, () => {
  test('should read from the cwd by default', async () => {
    const configFile = resolveFixture('js-config', 'jest.config.js');
    const rootDir = resolveFixture('js-config');
    jest.spyOn(process, 'cwd').mockReturnValue(rootDir);
    const {config, configPath} = await readInitialOptions();
    expect(config).toEqual({jestConfig: 'jest.config.js', rootDir});
    expect(configPath).toEqual(configFile);
  });
  test('should read a jest.config.js file', async () => {
    const configFile = resolveFixture('js-config', 'jest.config.js');
    const rootDir = resolveFixture('js-config');
    const {config, configPath} = await readInitialOptions(configFile);
    expect(config).toEqual({jestConfig: 'jest.config.js', rootDir});
    expect(configPath).toEqual(configFile);
  });
  test('should read a package.json file', async () => {
    const configFile = resolveFixture('pkg-config', 'package.json');
    const rootDir = resolveFixture('pkg-config');
    const {config, configPath} = await readInitialOptions(configFile);
    expect(config).toEqual({jestConfig: 'package.json', rootDir});
    expect(configPath).toEqual(configFile);
  });
  test('should read a jest.config.ts file', async () => {
    const configFile = resolveFixture('ts-config', 'jest.config.ts');
    const rootDir = resolveFixture('ts-config');
    // Read by proxy, because we're running in a VM and are not allowed to import 'ts-node' directly
    const {stdout} = await execa('node', ['../readOptions.js'], {
      cwd: rootDir,
    });

Review comment (suggestion):
You should spawn `node` inside all the fixtures (like you do for the TS and ESM one), not import and run from here

Code after:
/**
 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
import path = require('path');
import execa = require('execa');
import type {ReadJestConfigOptions, readInitialOptions} from 'jest-config';

function resolveFixture(...pathSegments: Array<string>) {
  return path.resolve(__dirname, '..', 'read-initial-options', ...pathSegments);
}

interface ProxyReadJestConfigOptions extends ReadJestConfigOptions {
  cwd?: string;
}

/**
 * These e2e tests are running via a child process, because we're running in a VM and are not allowed to `import` directly
 * It also represents a more real-world example of how to run.
 */
async function proxyReadInitialOptions(
  configFile: string | undefined,
  options: ProxyReadJestConfigOptions,
): ReturnType<typeof readInitialOptions> {
  const {stdout} = await execa(
    'node',
    [
      require.resolve('../read-initial-options/readOptions.js'),
      configFile ?? '',
      JSON.stringify(options),
    ],
    {cwd: options?.cwd},
  );
  return JSON.parse(stdout);
}

describe('readInitialOptions', () => {
  test('should read from the cwd by default', async () => {
    const configFile = resolveFixture('js-config', 'jest.config.js');
    const rootDir = resolveFixture('js-config');
    const {config, configPath} = await proxyReadInitialOptions(undefined, {
      cwd: rootDir,