- Configuring Jest·续5
- testMatch [array<string>]
- testPathIgnorePatterns [array<string>]
- testRegex [string | array<string>]
- testResultsProcessor [string]
- testRunner [string]
- testSequencer [string]
- testTimeout [number]
- testURL [string]
- timers [string]
- transform [object<string, pathToTransformer | [pathToTransformer, object]>]
- transformIgnorePatterns [array<string>]
Configuring Jest·续5
testMatch [array<string>]
(default: [ "/tests//.[jt]s?(x)", "/?(.)+(spec|test).[jt]s?(x)" ])
The glob patterns Jest uses to detect test files. By default it looks for .js, .jsx, .ts and .tsx files inside of tests folders, as well as any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js). It will also find files called test.js or spec.js.
See the micromatch package for details of the patterns you can specify.
See also testRegex [string | array<string>], but note that you cannot specify both options.
Note: Each glob pattern is applied in the order they are specified in the config. (For example [“!/fixtures/“, “/tests//.js”]will not excludefixturesbecause the negation is overwritten with the second pattern. In order to make the negated glob work in this example it has to come after/tests//.js.)
testPathIgnorePatterns [array<string>]
Default: ["/node_modules/"]
An array of regexp pattern strings that are matched against all test paths before executing the test. If the test path matches any of the patterns, it will be skipped.
These pattern strings match against the full path. Use the <rootDir> string token to include the path to your project’s root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories. Example: ["<rootDir>/build/", "<rootDir>/node_modules/"].
testRegex [string | array<string>]
Default: (/tests/.*|(\.|/)(test|spec))\.[jt]sx?$
The pattern or patterns Jest uses to detect test files. By default it looks for .js, .jsx, .ts and .tsx files inside of tests folders, as well as any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js). It will also find files called test.js or spec.js. See also testMatch [array<string>], but note that you cannot specify both options.
The following is a visualization of the default regex:
├── __tests__│ └── component.spec.js # test│ └── anything # test├── package.json # not test├── foo.test.js # test├── bar.spec.jsx # test└── component.js # not test
Note: testRegex will try to detect test files using the absolute file path, therefore, having a folder with a name that matches it will run all the files as tests
testResultsProcessor [string]
Default: undefined
This option allows the use of a custom results processor. This processor must be a node module that exports a function expecting an object with the following structure as the first argument and return it:
{ "success": boolean, "startTime": epoch, "numTotalTestSuites": number, "numPassedTestSuites": number, "numFailedTestSuites": number, "numRuntimeErrorTestSuites": number, "numTotalTests": number, "numPassedTests": number, "numFailedTests": number, "numPendingTests": number, "numTodoTests": number, "openHandles": Array<Error>, "testResults": [{ "numFailingTests": number, "numPassingTests": number, "numPendingTests": number, "testResults": [{ "title": string (message in it block), "status": "failed" | "pending" | "passed", "ancestorTitles": [string (message in describe blocks)], "failureMessages": [string], "numPassingAsserts": number, "location": { "column": number, "line": number } }, ... ], "perfStats": { "start": epoch, "end": epoch }, "testFilePath": absolute path to test file, "coverage": {} }, ... ]}
testRunner [string]
Default: jest-circus/runner
This option allows the use of a custom test runner. The default is jest-circus. A custom test runner can be provided by specifying a path to a test runner implementation.
The test runner module must export a function with the following signature:
function testRunner( globalConfig: GlobalConfig, config: ProjectConfig, environment: Environment, runtime: Runtime, testPath: string,): Promise<TestResult>;
An example of such function can be found in our default jasmine2 test runner package.
testSequencer [string]
Default: @jest/test-sequencer
This option allows you to use a custom sequencer instead of Jest’s default. sort may optionally return a Promise.
Example:
Sort test path alphabetically.
// testSequencer.jsconst Sequencer = require('@jest/test-sequencer').default;class CustomSequencer extends Sequencer { sort(tests) { // Test structure information // https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21 const copyTests = Array.from(tests); return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1)); }}module.exports = CustomSequencer;
Use it in your Jest config file like this:
{ "testSequencer": "path/to/testSequencer.js"}
testTimeout [number]
Default: 5000
Default timeout of a test in milliseconds.
testURL [string]
Default: http://localhost
This option sets the URL for the jsdom environment. It is reflected in properties such as location.href.
timers [string]
Default: real
Setting this value to fake or modern enables fake timers for all tests by default. Fake timers are useful when a piece of code sets a long timeout that we don’t want to wait for in a test. You can learn more about fake timers here.
If the value is legacy, the old implementation will be used as implementation instead of one backed by @sinonjs/fake-timers.
transform [object<string, pathToTransformer | [pathToTransformer, object]>]
Default: {"\.[jt]sx?$": "babel-jest"}
A map from regular expressions to paths to transformers. A transformer is a module that provides a synchronous function for transforming source files. For example, if you wanted to be able to use a new language feature in your modules or tests that aren’t yet supported by node, you might plug in one of many compilers that compile a future version of JavaScript to a current one. Example: see the examples/typescript example or the webpack tutorial.
Examples of such compilers include:
-
Babel
-
TypeScript
-
To build your own please visit the Custom Transformer section
You can pass configuration to a transformer like {filePattern: ['path-to-transformer', {options}]} For example, to configure babel-jest for non-default behavior, {"\.js$": ['babel-jest', {rootMode: "upward"}]}
Note: a transformer is only run once per file unless the file has changed. During the development of a transformer it can be useful to run Jest with --no-cache to frequently delete Jest’s cache.
Note: when adding additional code transformers, this will overwrite the default config and babel-jest is no longer automatically loaded. If you want to use it to compile JavaScript or Typescript, it has to be explicitly defined by adding {"\.[jt]sx?$": "babel-jest"} to the transform property. See babel-jest plugin
A transformer must be an object with at least a process function, and it’s also recommended to include a getCacheKey function. If your transformer is written in ESM you should have a default export with that object.
If the tests are written using native ESM the transformer can export processAsync and getCacheKeyAsync instead or in addition to the synchronous variants.
transformIgnorePatterns [array<string>]
Default: ["/node_modules/", "\.pnp\.[^\\/]+$"]
An array of regexp pattern strings that are matched against all source file paths before transformation. If the file path matches any of the patterns, it will not be transformed.
These pattern strings match against the full path. Use the <rootDir> string token to include the path to your project’s root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories.
Example: ["<rootDir>/bower_components/", "<rootDir>/node_modules/"].
