How to read jest config values within test?

Viewed 668

jest allows you to set global configuration values using either package.json, CLI options or a jest.config.js file.

You can set few config values in your tests e.g.

jest.setTimeout(10000000)

But I couldn't find a way to read config values, e.g.

const initialTimeout = jest.testTimeout // this is undefined
jest.setTimeout(10000000)
// do something that takes unusually long time
jest.setTimeout(initialTimeout)

So how can you read currently set global configuration values within a test?

1 Answers

You can import jest.config.js file in your test. This is the working code in my TypeScript package.

// jest.config.ts
export default {
  preset: 'ts-jest',
  testMatch: [
    '<rootDir>/tst/**/*.ts'
  ],
  transform: {
    '^.+\\.ts?$': 'ts-jest'
  },
  testEnvironment: 'jsdom',
  testURL: 'https://mytest.com',
}

// test.ts
import { default as jestConfig } from '../jest.config';

describe('my test', () => {
  test('get the testURL from jest config.', () => {
    expect(jestConfig.testURL).toBe('https://mytest.com');
  });
});
Related