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

Code before:
  });
  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,
    });
    const {config, configPath} = JSON.parse(stdout);
    expect(config).toEqual({jestConfig: 'jest.config.ts', rootDir});
    expect(configPath).toEqual(configFile);
  });
  test('should read a jest.config.mjs file', async () => {
    const configFile = resolveFixture('mjs-config', 'jest.config.mjs');
    const rootDir = resolveFixture('mjs-config');
    // Read by proxy, because we're running in a VM and are not allowed to `import` directly
    const {stdout} = await execa('node', ['../readOptions.js'], {
      cwd: rootDir,
    });
    const {config, configPath} = JSON.parse(stdout);
    expect(config).toEqual({jestConfig: 'jest.config.mjs', rootDir});
    expect(configPath).toEqual(configFile);
  });
  test('should read a jest.config.json file', async () => {
    const configFile = resolveFixture('json-config', 'jest.config.json');
    const rootDir = resolveFixture('json-config');
    const {config, configPath} = await readInitialOptions(configFile);
    expect(config).toEqual({jestConfig: 'jest.config.json', rootDir});
    expect(configPath).toEqual(configFile);
  });
  test('should read a jest config exporting an async function', async () => {
    const configFile = resolveFixture('async-config', 'jest.config.js');
    const rootDir = resolveFixture('async-config');
    const {config, configPath} = await readInitialOptions(configFile);
    expect(config).toEqual({jestConfig: 'async-config', rootDir});
    expect(configPath).toEqual(configFile);
  });

  test('should be able to skip config reading, instead read from cwd', async () => {
    const expectedConfigFile = resolveFixture(
      'json-config',
      'jest.config.json',
    );
    jest.spyOn(process, 'cwd').mockReturnValue(resolveFixture('json-config'));

Review comment (suggestion):
For my understanding this approach should be used in all test in this file. It is based on real world implementation and that is how I like e2e tests to be written.

In contrary, importing `readInitialOptions` to a test file and mocking `process.cwd` still feels like a unit test. Of course, that just an approach or opinion. The same thing is tested either way.

Code after:
      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,
    });
    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 proxyReadInitialOptions(undefined, {
      cwd: rootDir,
    });
    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 proxyReadInitialOptions(undefined, {
      cwd: rootDir,
    });
    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');
    const {config, configPath} = await proxyReadInitialOptions(undefined, {
      cwd: rootDir,
    });
    expect(config).toEqual({jestConfig: 'jest.config.ts', rootDir});
    expect(configPath).toEqual(configFile);
  });
  test('should read a jest.config.mjs file', async () => {
    const configFile = resolveFixture('mjs-config', 'jest.config.mjs');
    const rootDir = resolveFixture('mjs-config');
    const {config, configPath} = await proxyReadInitialOptions(undefined, {
      cwd: rootDir,