Jest 配置
Jest的理念在默认配置就能运行得很好,但有些时候我们还是需要发挥配置的功效。
为了方便对配置进行维护,建议在一个专用的Javascript、Typescript 或 JSON格式的配置文件中定义配置。 The file will be discovered automatically, if it is named jest.config.js|ts|mjs|cjs|cts|json. You can use --config flag to pass an explicit path to the file.
备注
注意配置文件最后导出的对象一定要是可被JSON序列化的
Open Config Examples
- Using
defineConfigfromjestyou should follow this:
- JavaScript
- TypeScript
jest.config.js
const {defineConfig} = require('jest');
module.exports = defineConfig({
// ... Specify options here.
});
jest.config.ts
/** @jest-config-loader ts-node */
// or
/** @jest-config-loader esbuild-register */
import {defineConfig} from 'jest';
export default defineConfig({
// ... Specify options here.
});
- You can retrieve Jest's defaults from
jest-configto extend them if needed:
- JavaScript
- TypeScript
jest.config.js
const {defineConfig} = require('jest');
const {defaults} = require('jest-config');
module.exports = defineConfig({
moduleDirectories: [...defaults.moduleDirectories, 'bower_components'],
});
jest.config.ts
/** @jest-config-loader ts-node */
// or
/** @jest-config-loader esbuild-register */
import {defineConfig} from 'jest';
import {defaults} from 'jest-config';
export default defineConfig({
moduleDirectories: [...defaults.moduleDirectories, 'bower_components'],
});
export default config;
- When using a separate Jest config, you can also extend Jest's options from another config file if needed using
mergeConfigfromjest:
- JavaScript
- TypeScript
jest.config.js
const {defineConfig, mergeConfig} = require('jest');
const jestConfig = require('./jest.config');
module.exports = mergeConfig(
jestConfig,
defineConfig({
// ... Specify options here.
}),
);
jest.config.ts
/** @jest-config-loader ts-node */
// or
/** @jest-config-loader esbuild-register */
import {defineConfig, mergeConfig} from 'jest';
import jestConfig from './jest.config';
export default mergeConfig(
jestConfig,
defineConfig({
// ... Specify options here.
}),
);
- If your Jest config needs to be defined as a function, you can define the config like this:
- JavaScript
- TypeScript
jest.config.js
const {defineConfig, mergeConfig} = require('jest');
const jestConfig = require('./jest.config');
module.exports = defineConfig(() =>
mergeConfig(
jestConfig,
defineConfig({
// ... Specify options here.
}),
),
);
jest.config.ts
/** @jest-config-loader ts-node */
// or
/** @jest-config-loader esbuild-register */
import {defineConfig, mergeConfig} from 'jest';
import jestConfig from './jest.config';
export default defineConfig(() =>
mergeConfig(
jestConfig,
defineConfig({
// ... Specify options here.
}),
),
);
- 你也可以将配置作为一个普通对象保存在JSON格式的文件里:
jest.config.json
{
"bail": 1,
"verbose": true
}
- Alternatively Jest's configuration can be defined through the
"jest"key in thepackage.jsonof your project:
package.json
{
"name": "my-project",
"jest": {
"verbose": true
}
}
- Also Jest's configuration json file can be referenced through the
"jest"key in thepackage.jsonof your project:
package.json
{
"name": "my-project",
"jest": "./path/to/config.json"
}
提示
To read TypeScript configuration files Jest by default requires ts-node. You can override this behavior by adding a @jest-config-loader docblock at the top of the file. Currently, ts-node and esbuild-register is supported. Make sure ts-node or the loader you specify is installed.
jest.config.ts
/** @jest-config-loader ts-node */
// or
/** @jest-config-loader esbuild-register */
import {defineConfig} from 'jest';
export default defineConfig({
verbose: true,
});
You can also pass options to the loader, for instance to enable transpileOnly.
jest.config.ts
/** @jest-config-loader ts-node */
/** @jest-config-loader-options {"transpileOnly": true} */
import type {defineConfig} from 'jest';
export default defineConfig({
verbose: true,
});
选项
automock[boolean]bail[number | boolean]cacheDirectory[string]clearMocks[boolean]collectCoverage[boolean]collectCoverageFrom[array]coverageDirectory[string]coveragePathIgnorePatterns[array<string>]coverageProvider[string]coverageReporters[array<string | [string, options]>]coverageThreshold[object]dependencyExtractor[string]displayName[string, object]errorOnDeprecated[boolean]extensionsToTreatAsEsm[array<string>]fakeTimers[object]forceCoverageMatch[array<string>]globals[object]globalSetup[string]globalTeardown[string]haste[object]injectGlobals[boolean]maxConcurrency[number]maxWorkers[number | string]moduleDirectories[array<string>]moduleFileExtensions[array<string>]moduleNameMapper[object<string, string | array<string>>]modulePathIgnorePatterns[array<string>]modulePaths[array<string>]notify[boolean]notifyMode[string]openHandlesTimeout[number]preset[string]prettierPath[string]projects[array<string | ProjectConfig>]randomize[boolean]reporters[array<moduleName | [moduleName, options]>]resetMocks[boolean]resetModules[boolean]resolver[string]restoreMocks[boolean]rootDir[string]roots[array<string>]runtime[string]runner[string]sandboxInjectedGlobals[array<string>]setupFiles[array]setupFilesAfterEnv[array]showSeed[boolean]slowTestThreshold[number]snapshotFormat[object]snapshotResolver[string]snapshotSerializers[array<string>]testEnvironment[string]testEnvironmentOptions[Object]testFailureExitCode[number]testMatch[array<string>]testPathIgnorePatterns[array<string>]testRegex[string | array<string>]testResultsProcessor[string]testRunner[string]testSequencer[string]testTimeout[number]transform[object<string, pathToTransformer | [pathToTransformer, object]>]transformIgnorePatterns[array<string>]unmockedModulePathPatterns[array<string>]verbose[boolean]waitForUnhandledRejections[boolean]watchPathIgnorePatterns[array<string>]watchPlugins[array<string | [string, Object]>]watchman[boolean]workerIdleMemoryLimit[number|string]//[string]workerThreads