Configuring Jest
The Jest philosophy is to work great by default, but sometimes you just need more configuration power.
It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it is namedjest.config.js|ts|mjs|cjs|cts|json
. You can use--config
flag to pass an explicit path to the file.
Keep in mind that the resulting configuration object must always be JSON-serializable.
The configuration file should simply export an object:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
verbose:true,
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
verbose:true,
};
exportdefault config;
Or a function returning an object:
- JavaScript
- TypeScript
/**@returns{Promise<import('jest').Config>} */
module.exports=async()=>{
return{
verbose:true,
};
};
importtype{Config}from'jest';
exportdefaultasync():Promise<Config>=>{
return{
verbose:true,
};
};
To read TypeScript configuration files Jest by default requirests-node
. You can override this behavior by adding a@jest-config-loader
docblock at the top of the file. Currently,ts-node
andesbuild-register
is supported. Make surets-node
or the loader you specify is installed.
/**@jest-config-loader ts-node */
// or
/**@jest-config-loader esbuild-register */
importtype{Config}from'jest';
const config: Config={
verbose:true,
};
exportdefault config;
You can also pass options to the loader, for instance to enabletranspileOnly
.
/**@jest-config-loader ts-node */
/**@jest-config-loader-options{"transpileOnly": true} */
importtype{Config}from'jest';
const config: Config={
verbose:true,
};
exportdefault config;
The configuration also can be stored in a JSON file as a plain object:
{
"bail":1,
"verbose":true
}
Alternatively Jest's configuration can be defined through the"jest"
key in thepackage.json
of your project:
{
"name":"my-project",
"jest":{
"verbose":true
}
}
Also Jest's configuration json file can be referenced through the"jest"
key in thepackage.json
of your project:
{
"name":"my-project",
"jest":"./path/to/config.json"
}
Options
You can retrieve Jest's defaults fromjest-config
to extend them if needed:
- JavaScript
- TypeScript
const{defaults}=require('jest-config');
/**@type{import('jest').Config} */
const config={
moduleDirectories:[...defaults.moduleDirectories,'bower_components'],
};
module.exports= config;
importtype{Config}from'jest';
import{defaults}from'jest-config';
const config: Config={
moduleDirectories:[...defaults.moduleDirectories,'bower_components'],
};
exportdefault config;
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>]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
Reference
automock
[boolean]
Default:false
This option tells Jest that all imported modules in your tests should be mocked automatically. All modules used in your tests will have a replacement implementation, keeping the API surface.
Example:
exportdefault{
authorize:()=>'token',
isAuthorized:secret=> secret==='wizard',
};
importutilsfrom'../utils';
test('if utils mocked automatically',()=>{
// Public methods of `utils` are now mock functions
expect(utils.authorize.mock).toBeTruthy();
expect(utils.isAuthorized.mock).toBeTruthy();
// You can provide them with your own implementation
// or pass the expected return value
utils.authorize.mockReturnValue('mocked_token');
utils.isAuthorized.mockReturnValue(true);
expect(utils.authorize()).toBe('mocked_token');
expect(utils.isAuthorized('not_wizard')).toBeTruthy();
});
Node modules are automatically mocked when you have a manual mock in place (e.g.:__mocks__/lodash.js
). More infohere.
Node.js core modules, likefs
, are not mocked by default. They can be mocked explicitly, likejest.mock('fs')
.
bail
[number | boolean]
Default:0
By default, Jest runs all tests and produces all errors into the console upon completion. The bail config option can be used here to have Jest stop running tests aftern
failures. Setting bail totrue
is the same as setting bail to1
.
cacheDirectory
[string]
Default:"/tmp/<path>"
The directory where Jest should store its cached dependency information.
Jest attempts to scan your dependency tree once (up-front) and cache it in order to ease some of the filesystem churn that needs to happen while running tests. This config option lets you customize where Jest stores that cache data on disk.
clearMocks
[boolean]
Default:false
Automatically clear mock calls, instances, contexts and results before every test. Equivalent to callingjest.clearAllMocks()
before each test. This does not remove any mock implementation that may have been provided.
collectCoverage
[boolean]
Default:false
Indicates whether the coverage information should be collected while executing the test. Because this retrofits all executed files with coverage collection statements, it may significantly slow down your tests.
Jest ships with two coverage providers:babel
(default) andv8
. See thecoverageProvider
option for more details.
Thebabel
andv8
coverage providers use/* istanbul ignore next */
and/* c8 ignore next */
comments to exclude lines from coverage reports, respectively. For more information, you can view theistanbuljs
documentation and thec8
documentation.
collectCoverageFrom
[array]
Default:undefined
An array ofglob patterns indicating a set of files for which coverage information should be collected. If a file matches the specified glob pattern, coverage information will be collected for it even if no tests exist for this file and it's never required in the test suite.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
collectCoverageFrom:[
'**/*.{js,jsx}',
'!**/node_modules/**',
'!**/vendor/**',
],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
collectCoverageFrom:[
'**/*.{js,jsx}',
'!**/node_modules/**',
'!**/vendor/**',
],
};
exportdefault config;
This will collect coverage information for all the files inside the project'srootDir
, except the ones that match**/node_modules/**
or**/vendor/**
.
Each glob pattern is applied in the order they are specified in the config. For example["!**/__tests__/**", "**/*.js"]
will not exclude__tests__
because the negation is overwritten with the second pattern. In order to make the negated glob work in this example it has to come after**/*.js
.
This option requirescollectCoverage
to be set totrue
or Jest to be invoked with--coverage
.
Help:
If you are seeing coverage output such as...
=============================== Coverage summary ===============================
Statements : Unknown% ( 0/0 )
Branches : Unknown% ( 0/0 )
Functions : Unknown% ( 0/0 )
Lines : Unknown% ( 0/0 )
================================================================================
Jest: Coverage data for global was not found.
Most likely your glob patterns are not matching any files. Refer to themicromatch documentation to ensure your globs are compatible.
coverageDirectory
[string]
Default:undefined
The directory where Jest should output its coverage files.
coveragePathIgnorePatterns
[array<string>]
Default:["/node_modules/"]
An array of regexp pattern strings that are matched against all file paths before executing the test. If the file path matches any of the patterns, coverage information 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/"]
.
coverageProvider
[string]
Indicates which provider should be used to instrument code for coverage. Allowed values arebabel
(default) orv8
.
coverageReporters
[array<string | [string, options]>]
Default:["clover", "json", "lcov", "text"]
A list of reporter names that Jest uses when writing coverage reports. Anyistanbul reporter can be used.
Setting this option overwrites the default values. Add"text"
or"text-summary"
to see a coverage summary in the console output.
Additional options can be passed using the tuple form. For example, you may hide coverage report lines for all fully-covered files:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
coverageReporters:['clover','json','lcov',['text',{skipFull:true}]],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
coverageReporters:['clover','json','lcov',['text',{skipFull:true}]],
};
exportdefault config;
For more information about the options object shape refer toCoverageReporterWithOptions
type in thetype definitions.
coverageThreshold
[object]
Default:undefined
This will be used to configure minimum threshold enforcement for coverage results. Thresholds can be specified asglobal
, as aglob, and as a directory or file path. If thresholds aren't met, jest will fail. Thresholds specified as a positive number are taken to be the minimum percentage required. Thresholds specified as a negative number represent the maximum number of uncovered entities allowed.
For example, with the following configuration jest will fail if there is less than 80% branch, line, and function coverage, or if there are more than 10 uncovered statements:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
coverageThreshold:{
global:{
branches:80,
functions:80,
lines:80,
statements:-10,
},
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
coverageThreshold:{
global:{
branches:80,
functions:80,
lines:80,
statements:-10,
},
},
};
exportdefault config;
If globs or paths are specified alongsideglobal
, coverage data for matching paths will be subtracted from overall coverage and thresholds will be applied independently. Thresholds for globs are applied to all files matching the glob. If the file specified by path is not found, an error is returned.
For example, with the following configuration:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
coverageThreshold:{
global:{
branches:50,
functions:50,
lines:50,
statements:50,
},
'./src/components/':{
branches:40,
statements:40,
},
'./src/reducers/**/*.js':{
statements:90,
},
'./src/api/very-important-module.js':{
branches:100,
functions:100,
lines:100,
statements:100,
},
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
coverageThreshold:{
global:{
branches:50,
functions:50,
lines:50,
statements:50,
},
'./src/components/':{
branches:40,
statements:40,
},
'./src/reducers/**/*.js':{
statements:90,
},
'./src/api/very-important-module.js':{
branches:100,
functions:100,
lines:100,
statements:100,
},
},
};
exportdefault config;
Jest will fail if:
- The
./src/components
directory has less than 40% branch or statement coverage. - One of the files matching the
./src/reducers/**/*.js
glob has less than 90% statement coverage. - The
./src/api/very-important-module.js
file has less than 100% coverage. - Every remaining file combined has less than 50% coverage (
global
).
dependencyExtractor
[string]
Default:undefined
This option allows the use of a custom dependency extractor. It must be a node module that exports an object with anextract
function. E.g.:
const crypto=require('crypto');
const fs=require('fs');
module.exports={
extract(code, filePath, defaultExtract){
const deps=defaultExtract(code, filePath);
// Scan the file and add dependencies in `deps` (which is a `Set`)
return deps;
},
getCacheKey(){
return crypto
.createHash('md5')
.update(fs.readFileSync(__filename))
.digest('hex');
},
};
Theextract
function should return an iterable (Array
,Set
, etc.) with the dependencies found in the code.
That module can also contain agetCacheKey
function to generate a cache key to determine if the logic has changed and any cached artifacts relying on it should be discarded.
displayName
[string, object]
default:undefined
Allows for a label to be printed alongside a test while it is running. This becomes more useful in multi-project repositories where there can be many jest configuration files. This visually tells which project a test belongs to.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
displayName:'CLIENT',
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
displayName:'CLIENT',
};
exportdefault config;
Alternatively, an object with the propertiesname
andcolor
can be passed. This allows for a custom configuration of the background color of the displayName.displayName
defaults to white when its value is a string. Jest useschalk
to provide the color. As such, all of the valid options for colors supported bychalk
are also supported by Jest.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
displayName:{
name:'CLIENT',
color:'blue',
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
displayName:{
name:'CLIENT',
color:'blue',
},
};
exportdefault config;
errorOnDeprecated
[boolean]
Default:false
Make calling deprecated APIs throw helpful error messages. Useful for easing the upgrade process.
extensionsToTreatAsEsm
[array<string>]
Default:[]
Jest will run.mjs
and.js
files with nearestpackage.json
'stype
field set tomodule
as ECMAScript Modules. If you have any other files that should run with native ESM, you need to specify their file extension here.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
extensionsToTreatAsEsm:['.ts'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
extensionsToTreatAsEsm:['.ts'],
};
exportdefault config;
Jest's ESM support is still experimental, seeits docs for more details.
fakeTimers
[object]
Default:{}
The fake timers may be useful when a piece of code sets a long timeout that we don't want to wait for in a test. For additional details seeFake Timers guide andAPI documentation.
This option provides the default configuration of fake timers for all tests. Callingjest.useFakeTimers()
in a test file will use these options or will override them if a configuration object is passed. For example, you can tell Jest to keep the original implementation ofprocess.nextTick()
and adjust the limit of recursive timers that will be run:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
fakeTimers:{
doNotFake:['nextTick'],
timerLimit:1000,
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
fakeTimers:{
doNotFake:['nextTick'],
timerLimit:1000,
},
};
exportdefault config;
// install fake timers for this file using the options from Jest configuration
jest.useFakeTimers();
test('increase the limit of recursive timers for this and following tests',()=>{
jest.useFakeTimers({timerLimit:5000});
// ...
});
Instead of includingjest.useFakeTimers()
in each test file, you can enable fake timers globally for all tests in your Jest configuration:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
fakeTimers:{
enableGlobally:true,
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
fakeTimers:{
enableGlobally:true,
},
};
exportdefault config;
Configuration options:
typeFakeableAPI=
|'Date'
|'hrtime'
|'nextTick'
|'performance'
|'queueMicrotask'
|'requestAnimationFrame'
|'cancelAnimationFrame'
|'requestIdleCallback'
|'cancelIdleCallback'
|'setImmediate'
|'clearImmediate'
|'setInterval'
|'clearInterval'
|'setTimeout'
|'clearTimeout';
typeModernFakeTimersConfig={
/**
* If set to `true` all timers will be advanced automatically by 20 milliseconds
* every 20 milliseconds. A custom time delta may be provided by passing a number.
* The default is `false`.
*/
advanceTimers?:boolean|number;
/**
* List of names of APIs that should not be faked. The default is `[]`, meaning
* all APIs are faked.
*/
doNotFake?:Array<FakeableAPI>;
/** Whether fake timers should be enabled for all test files. The default is `false`. */
enableGlobally?:boolean;
/**
* Use the old fake timers implementation instead of one backed by `@sinonjs/fake-timers`.
* The default is `false`.
*/
legacyFakeTimers?:boolean;
/** Sets current system time to be used by fake timers, in milliseconds. The default is `Date.now()`. */
now?:number;
/** Maximum number of recursive timers that will be run. The default is `100_000` timers. */
timerLimit?:number;
};
For some reason you might have to use legacy implementation of fake timers. Here is how to enable it globally (additional options are not supported):
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
fakeTimers:{
enableGlobally:true,
legacyFakeTimers:true,
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
fakeTimers:{
enableGlobally:true,
legacyFakeTimers:true,
},
};
exportdefault config;
forceCoverageMatch
[array<string>]
Default:['']
Test files are normally ignored from collecting code coverage. With this option, you can overwrite this behavior and include otherwise ignored files in code coverage.
For example, if you have tests in source files named with.t.js
extension as following:
exportfunctionsum(a, b){
return a+ b;
}
if(process.env.NODE_ENV==='test'){
test('sum',()=>{
expect(sum(1,2)).toBe(3);
});
}
You can collect coverage from those files with settingforceCoverageMatch
.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
forceCoverageMatch:['**/*.t.js'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
forceCoverageMatch:['**/*.t.js'],
};
exportdefault config;
globals
[object]
Default:{}
A set of global variables that need to be available in all test environments.
For example, the following would create a global__DEV__
variable set totrue
in all test environments:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
globals:{
__DEV__:true,
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
globals:{
__DEV__:true,
},
};
exportdefault config;
If you specify a global reference value (like an object or array) here, and some code mutates that value in the midst of running a test, that mutation willnot be persisted across test runs for other test files. In addition, theglobals
object must be json-serializable, so it can't be used to specify global functions. For that, you should usesetupFiles
.
globalSetup
[string]
Default:undefined
This option allows the use of a custom global setup module, which must export a function (it can be sync or async). The function will be triggered once before all test suites and it will receive two arguments: Jest'sglobalConfig
andprojectConfig
.
A global setup module configured in a project (using multi-project runner) will be triggered only when you run at least one test from this project.
Any global variables that are defined throughglobalSetup
can only be read inglobalTeardown
. You cannot retrieve globals defined here in your test suites.
While code transformation is applied to the linked setup-file, Jest willnot transform any code innode_modules
. This is due to the need to load the actual transformers (e.g.babel
ortypescript
) to perform transformation.
module.exports=asyncfunction(globalConfig, projectConfig){
console.log(globalConfig.testPathPatterns);
console.log(projectConfig.cache);
// Set reference to mongod in order to close the server during teardown.
globalThis.__MONGOD__= mongod;
};
module.exports=asyncfunction(globalConfig, projectConfig){
console.log(globalConfig.testPathPatterns);
console.log(projectConfig.cache);
await globalThis.__MONGOD__.stop();
};
globalTeardown
[string]
Default:undefined
This option allows the use of a custom global teardown module which must export a function (it can be sync or async). The function will be triggered once after all test suites and it will receive two arguments: Jest'sglobalConfig
andprojectConfig
.
A global teardown module configured in a project (using multi-project runner) will be triggered only when you run at least one test from this project.
The same caveat concerning transformation ofnode_modules
as forglobalSetup
applies toglobalTeardown
.
haste
[object]
Default:undefined
This will be used to configure the behavior ofjest-haste-map
, Jest's internal file crawler/cache system. The following options are supported:
typeHasteConfig={
/** Whether to hash files using SHA-1. */
computeSha1?:boolean;
/** The platform to use as the default, e.g. 'ios'. */
defaultPlatform?:string|null;
/** Force use of Node's `fs` APIs rather than shelling out to `find` */
forceNodeFilesystemAPI?:boolean;
/**
* Whether to follow symlinks when crawling for files.
* This options cannot be used in projects which use watchman.
* Projects with `watchman` set to true will error if this option is set to true.
*/
enableSymlinks?:boolean;
/** Path to a custom implementation of Haste. */
hasteImplModulePath?:string;
/** All platforms to target, e.g ['ios', 'android']. */
platforms?:Array<string>;
/** Whether to throw an error on module collision. */
throwOnModuleCollision?:boolean;
/** Custom HasteMap module */
hasteMapModulePath?:string;
/** Whether to retain all files, allowing e.g. search for tests in `node_modules`. */
retainAllFiles?:boolean;
};
injectGlobals
[boolean]
Default:true
Insert Jest's globals (expect
,test
,describe
,beforeEach
etc.) into the global environment. If you set this tofalse
, you should import from@jest/globals
, e.g.
import{expect, jest, test}from'@jest/globals';
jest.useFakeTimers();
test('some test',()=>{
expect(Date.now()).toBe(0);
});
This option is only supported using the defaultjest-circus
test runner.
maxConcurrency
[number]
Default:5
A number limiting the number of tests that are allowed to run at the same time when usingtest.concurrent
. Any test above this limit will be queued and executed once a slot is released.
maxWorkers
[number | string]
Specifies the maximum number of workers the worker-pool will spawn for running tests. In single run mode, this defaults to the number of the cores available on your machine minus one for the main thread. In watch mode, this defaults to half of the available cores on your machine to ensure Jest is unobtrusive and does not grind your machine to a halt. It may be useful to adjust this in resource limited environments like CIs but the defaults should be adequate for most use-cases.
For environments with variable CPUs available, you can use percentage based configuration:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
maxWorkers:'50%',
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
maxWorkers:'50%',
};
exportdefault config;
moduleDirectories
[array<string>]
Default:["node_modules"]
An array of directory names to be searched recursively up from the requiring module's location. Setting this option willoverride the default, if you wish to still searchnode_modules
for packages include it along with any other options:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
moduleDirectories:['node_modules','bower_components'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
moduleDirectories:['node_modules','bower_components'],
};
exportdefault config;
It is discouraged to use'.'
as one of themoduleDirectories
, because this prevents scoped packages such as@emotion/react
from accessing packages with the same subdirectory name (react
). Seethis issue for more details. In most cases, it is preferable to use themoduleNameMapper configuration instead.
moduleFileExtensions
[array<string>]
Default:["js", "mjs", "cjs", "jsx", "ts", "mts", "cts", "tsx", "json", "node"]
An array of file extensions your modules use. If you require modules without specifying a file extension, these are the extensions Jest will look for, in left-to-right order.
We recommend placing the extensions most commonly used in your project on the left, so if you are using TypeScript, you may want to consider moving "ts" and/or "tsx" to the beginning of the array.
moduleNameMapper
[object<string, string | array<string>>]
Default:null
A map from regular expressions to module names or to arrays of module names that allow to stub out resources, like images or styles with a single module.
Modules that are mapped to an alias are unmocked by default, regardless of whether automocking is enabled or not.
Use<rootDir>
string token to refer torootDir
value if you want to use file paths.
Additionally, you can substitute captured regex groups using numbered backreferences.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
moduleNameMapper:{
'^image![a-zA-Z0-9$_-]+$':'GlobalImageStub',
'^[./a-zA-Z0-9$_-]+\\.png$':'<rootDir>/RelativeImageStub.js',
'module_name_(.*)':'<rootDir>/substituted_module_$1.js',
'assets/(.*)':[
'<rootDir>/images/$1',
'<rootDir>/photos/$1',
'<rootDir>/recipes/$1',
],
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
moduleNameMapper:{
'^image![a-zA-Z0-9$_-]+$':'GlobalImageStub',
'^[./a-zA-Z0-9$_-]+\\.png$':'<rootDir>/RelativeImageStub.js',
'module_name_(.*)':'<rootDir>/substituted_module_$1.js',
'assets/(.*)':[
'<rootDir>/images/$1',
'<rootDir>/photos/$1',
'<rootDir>/recipes/$1',
],
},
};
exportdefault config;
The order in which the mappings are defined matters. Patterns are checked one by one until one fits. The most specific rule should be listed first. This is true for arrays of module names as well.
If you provide module names without boundaries^$
it may cause hard to spot errors. E.g.relay
will replace all modules which containrelay
as a substring in its name:relay
,react-relay
andgraphql-relay
will all be pointed to your stub.
modulePathIgnorePatterns
[array<string>]
Default:[]
An array of regexp pattern strings that are matched against all module paths before those paths are to be considered 'visible' to the module loader. If a given module's path matches any of the patterns, it will not berequire()
-able in the test environment.
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.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
modulePathIgnorePatterns:['<rootDir>/build/'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
modulePathIgnorePatterns:['<rootDir>/build/'],
};
exportdefault config;
modulePaths
[array<string>]
Default:[]
An alternative API to setting theNODE_PATH
env variable,modulePaths
is an array of absolute paths to additional locations to search when resolving modules. Use the<rootDir>
string token to include the path to your project's root directory.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
modulePaths:['<rootDir>/app/'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
modulePaths:['<rootDir>/app/'],
};
exportdefault config;
notify
[boolean]
Default:false
Activates native OS notifications for test results. To display the notifications Jest needsnode-notifier
package, which must be installed additionally:
- npm
- Yarn
- pnpm
npminstall --save-dev node-notifier
yarnadd--dev node-notifier
pnpmadd --save-dev node-notifier
On macOS, remember to allow notifications fromterminal-notifier
under System Preferences > Notifications & Focus.
On Windows,node-notifier
creates a new start menu entry on the first use and not display the notification. Notifications will be properly displayed on subsequent runs.
notifyMode
[string]
Default:failure-change
Specifies notification mode. Requiresnotify: true
.
Modes
always
: always send a notification.failure
: send a notification when tests fail.success
: send a notification when tests pass.change
: send a notification when the status changed.success-change
: send a notification when tests pass or once when it fails.failure-change
: send a notification when tests fail or once when it passes.
openHandlesTimeout
[number]
Default:1000
Print a warning indicating that there are probable open handles if Jest does not exit cleanly this number of milliseconds after it completes. Use0
to disable the warning.
preset
[string]
Default:undefined
A preset that is used as a base for Jest's configuration. A preset should point to an npm module that has ajest-preset.json
,jest-preset.js
,jest-preset.cjs
orjest-preset.mjs
file at the root.
For example, this presetfoo-bar/jest-preset.js
will be configured as follows:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
preset:'foo-bar',
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
preset:'foo-bar',
};
exportdefault config;
Presets may also be relative to filesystem paths:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
preset:'./node_modules/foo-bar/jest-preset.js',
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
preset:'./node_modules/foo-bar/jest-preset.js',
};
exportdefault config;
If you also have specifiedrootDir
, the resolution of this file will be relative to that root directory.
prettierPath
[string]
Default:'prettier'
Sets the path to theprettier
node module used to update inline snapshots.
projects
[array<string | ProjectConfig>]
Default:undefined
When theprojects
configuration is provided with an array of paths or glob patterns, Jest will run tests in all of the specified projects at the same time. This is great for monorepos or when working on multiple projects at the same time.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
projects:['<rootDir>','<rootDir>/examples/*'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
projects:['<rootDir>','<rootDir>/examples/*'],
};
exportdefault config;
This example configuration will run Jest in the root directory as well as in every folder in the examples directory. You can have an unlimited amount of projects running in the same Jest instance.
The projects feature can also be used to run multiple configurations or multiplerunners. For this purpose, you can pass an array of configuration objects. For example, to run both tests and ESLint (viajest-runner-eslint) in the same invocation of Jest:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
projects:[
{
displayName:'test',
},
{
displayName:'lint',
runner:'jest-runner-eslint',
testMatch:['<rootDir>/**/*.js'],
},
],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
projects:[
{
displayName:'test',
},
{
displayName:'lint',
runner:'jest-runner-eslint',
testMatch:['<rootDir>/**/*.js'],
},
],
};
exportdefault config;
When using multi-project runner, it's recommended to add adisplayName
for each project. This will show thedisplayName
of a project next to its tests.
With theprojects
option enabled, Jest will copy the root-level configuration options to each individual child configuration during the test run, resolving its values in the child's context. This means that string tokens like<rootDir>
will point to thechild's root directory even if they are defined in the root-level configuration.
randomize
[boolean]
Default:false
The equivalent of the--randomize
flag to randomize the order of the tests in a file.
reporters
[array<moduleName | [moduleName, options]>]
Default:undefined
Use this configuration option to add reporters to Jest. It must be a list of reporter names, additional options can be passed to a reporter using the tuple form:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
reporters:[
'default',
['<rootDir>/custom-reporter.js',{banana:'yes',pineapple:'no'}],
],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
reporters:[
'default',
['<rootDir>/custom-reporter.js',{banana:'yes', pineapple:'no'}],
],
};
exportdefault config;
Default Reporter
If custom reporters are specified, the default Jest reporter will be overridden. If you wish to keep it,'default'
must be passed as a reporters name:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
reporters:[
'default',
['jest-junit',{outputDirectory:'reports',outputName:'report.xml'}],
],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
reporters:[
'default',
['jest-junit',{outputDirectory:'reports', outputName:'report.xml'}],
],
};
exportdefault config;
GitHub Actions Reporter
If included in the list, the built-in GitHub Actions Reporter will annotate changed files with test failure messages and (if used with'silent: false'
) print logs with github group features for easy navigation. Note that'default'
should not be used in this case as'github-actions'
will handle that already, so remember to also include'summary'
. If you wish to use it only for annotations simply leave only the reporter without options as the default value of'silent'
is'true'
:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
reporters:[['github-actions',{silent:false}],'summary'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
reporters:[['github-actions',{silent:false}],'summary'],
};
exportdefault config;
Summary Reporter
Summary reporter prints out summary of all tests. It is a part of default reporter, hence it will be enabled if'default'
is included in the list. For instance, you might want to use it as stand-alone reporter instead of the default one, or together withSilent Reporter:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
reporters:['jest-silent-reporter','summary'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
reporters:['jest-silent-reporter','summary'],
};
exportdefault config;
Thesummary
reporter accepts options. Since it is included in thedefault
reporter you may also pass the options there.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
reporters:[['default',{summaryThreshold:10}]],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
reporters:[['default',{summaryThreshold:10}]],
};
exportdefault config;
ThesummaryThreshold
option behaves in the following way, if the total number of test suites surpasses this threshold, a detailed summary of all failed tests will be printed after executing all the tests. It defaults to20
.
Custom Reporters
Hungry for reporters? Take a look at long list ofawesome reporters from Awesome Jest.
Custom reporter module must export a class that takesglobalConfig
,reporterOptions
andreporterContext
as constructor arguments:
classCustomReporter{
constructor(globalConfig, reporterOptions, reporterContext){
this._globalConfig= globalConfig;
this._options= reporterOptions;
this._context= reporterContext;
}
onRunComplete(testContexts, results){
console.log('Custom reporter output:');
console.log('global config:',this._globalConfig);
console.log('options for this reporter from Jest config:',this._options);
console.log('reporter context passed from test scheduler:',this._context);
}
// Optionally, reporters can force Jest to exit with non zero code by returning
// an `Error` from `getLastError()` method.
getLastError(){
if(this._shouldFail){
returnnewError('Custom error reported!');
}
}
}
module.exports=CustomReporter;
For the full list of hooks and argument types see theReporter
interface inpackages/jest-reporters/src/types.ts.
resetMocks
[boolean]
Default:false
Automatically reset mock state before every test. Equivalent to callingjest.resetAllMocks()
before each test. This will lead to any mocks having their fake implementations removed but does not restore their initial implementation.
resetModules
[boolean]
Default:false
By default, each test file gets its own independent module registry. EnablingresetModules
goes a step further and resets the module registry before running each individual test. This is useful to isolate modules for every test so that the local module state doesn't conflict between tests. This can be done programmatically usingjest.resetModules()
.
resolver
[string]
Default:undefined
This option allows the use of a custom resolver. This resolver must be a module that exportseither:
- a function expecting a string as the first argument for the path to resolve and an options object as the second argument. The function should either return a path to the module that should be resolved or throw an error if the module can't be found.or
- an object containing
async
and/orsync
properties. Thesync
property should be a function with the shape explained above, and theasync
property should also be a function that accepts the same arguments, but returns a promise which resolves with the path to the module or rejects with an error.
The options object provided to resolvers has the shape:
typeResolverOptions={
/** Directory to begin resolving from. */
basedir:string;
/** List of export conditions. */
conditions?:Array<string>;
/** Instance of default resolver. */
defaultResolver:(path:string, options: ResolverOptions)=>string;
/** List of file extensions to search in order. */
extensions?:Array<string>;
/** List of directory names to be looked up for modules recursively. */
moduleDirectory?:Array<string>;
/** List of `require.paths` to use if nothing is found in `node_modules`. */
paths?:Array<string>;
/** Current root directory. */
rootDir?:string;
};
ThedefaultResolver
passed as an option is the Jest default resolver which might be useful when you write your custom one. It takes the same arguments as your custom synchronous one, e.g.(path, options)
and returns a string or throws.
For example, if you want to respect Browserify's"browser"
field, you can use the following resolver:
const browserResolve=require('browser-resolve');
module.exports= browserResolve.sync;
And add it to Jest configuration:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
resolver:'<rootDir>/resolver.js',
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
resolver:'<rootDir>/resolver.js',
};
exportdefault config;
By combiningdefaultResolver
andpackageFilter
we can implement apackage.json
"pre-processor" that allows us to change how the default resolver will resolve modules. For example, imagine we want to use the field"module"
if it is present, otherwise fallback to"main"
:
module.exports=(path, options)=>{
// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path,{
...options,
// Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
packageFilter:pkg=>{
return{
...pkg,
// Alter the value of `main` before resolving the package
main: pkg.module|| pkg.main,
};
},
});
};
restoreMocks
[boolean]
Default:false
Automatically restore mock state and implementation before every test. Equivalent to callingjest.restoreAllMocks()
before each test. This will lead to any mocks having their fake implementations removed and restores their initial implementation.
rootDir
[string]
Default: The root of the directory containing your Jestconfig fileor thepackage.json
or thepwd
if nopackage.json
is found
The root directory that Jest should scan for tests and modules within. If you put your Jest config inside yourpackage.json
and want the root directory to be the root of your repo, the value for this config param will default to the directory of thepackage.json
.
Oftentimes, you'll want to set this to'src'
or'lib'
, corresponding to where in your repository the code is stored.
Using'<rootDir>'
as a string token in any other path-based configuration settings will refer back to this value. For example, if you want asetupFiles
entry to point at thesome-setup.js
file at the root of the project, set its value to:'<rootDir>/some-setup.js'
.
roots
[array<string>]
Default:["<rootDir>"]
A list of paths to directories that Jest should use to search for files in.
There are times where you only want Jest to search in a single sub-directory (such as cases where you have asrc/
directory in your repo), but prevent it from accessing the rest of the repo.
WhilerootDir
is mostly used as a token to be re-used in other configuration options,roots
is used by the internals of Jest to locatetest files and source files. This applies also when searching for manual mocks for modules fromnode_modules
(__mocks__
will need to live in one of theroots
).
By default,roots
has a single entry<rootDir>
but there are cases where you may want to have multiple roots within one project, for exampleroots: ["<rootDir>/src/", "<rootDir>/tests/"]
.
runner
[string]
Default:"jest-runner"
This option allows you to use a custom runner instead of Jest's default test runner. Examples of runners include:
Therunner
property value can omit thejest-runner-
prefix of the package name.
To write a test-runner, export a class with which acceptsglobalConfig
in the constructor, and has arunTests
method with the signature:
asyncfunctionrunTests(
tests:Array<Test>,
watcher: TestWatcher,
onStart: OnTestStart,
onResult: OnTestSuccess,
onFailure: OnTestFailure,
options: TestRunnerOptions,
):Promise<void>;
If you need to restrict your test-runner to only run in serial rather than being executed in parallel your class should have the propertyisSerial
to be set astrue
.
sandboxInjectedGlobals
[array<string>]
Renamed fromextraGlobals
in Jest 28.
Default:undefined
Test files run inside avm, which slows calls to global context properties (e.g.Math
). With this option you can specify extra properties to be defined inside the vm for faster lookups.
For example, if your tests callMath
often, you can pass it by settingsandboxInjectedGlobals
.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
sandboxInjectedGlobals:['Math'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
sandboxInjectedGlobals:['Math'],
};
exportdefault config;
This option has no effect if you usenative ESM.
setupFiles
[array]
Default:[]
A list of paths to modules that run some code to configure or set up the testing environment. Each setupFile will be run once per test file. Since every test runs in its own environment, these scripts will be executed in the testing environment before executingsetupFilesAfterEnv
and before the test code itself.
If your setup script is a CJS module, it may export an async function. Jest will call the function and await its result. This might be useful to fetch some data asynchronously. If the file is an ESM module, simply use top-level await to achieve the same result.
setupFilesAfterEnv
[array]
Default:[]
A list of paths to modules that run some code to configure or set up the testing framework before each test file in the suite is executed. SincesetupFiles
executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment but before the test code itself.
In other words,setupFilesAfterEnv
modules are meant for code which is repeating in each test file. Having the test framework installed makes Jestglobals,jest
object andexpect
accessible in the modules. For example, you can add extra matchers fromjest-extended
library or callsetup and teardown hooks:
const matchers=require('jest-extended');
expect.extend(matchers);
afterEach(()=>{
jest.useRealTimers();
});
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
setupFilesAfterEnv:['<rootDir>/setup-jest.js'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
setupFilesAfterEnv:['<rootDir>/setup-jest.js'],
};
exportdefault config;
If your setup script is a CJS module, it may export an async function. Jest will call the function and await its result. This might be useful to fetch some data asynchronously. If the file is an ESM module, simply use top-level await to achieve the same result.
showSeed
[boolean]
Default:false
The equivalent of the--showSeed
flag to print the seed in the test report summary.
slowTestThreshold
[number]
Default:5
The number of seconds after which a test is considered as slow and reported as such in the results.
snapshotFormat
[object]
Default:{escapeString: false, printBasicPrototype: false}
Allows overriding specific snapshot formatting options documented in thepretty-format readme, with the exceptions ofcompareKeys
andplugins
. For example, this config would have the snapshot formatter not print a prefix for "Object" and "Array":
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
snapshotFormat:{
printBasicPrototype:false,
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
snapshotFormat:{
printBasicPrototype:false,
},
};
exportdefault config;
test('does not show prototypes for object and array inline',()=>{
const object={
array:[{hello:'Danger'}],
};
expect(object).toMatchInlineSnapshot(`
{
"array": [
{
"hello": "Danger",
},
],
}
`);
});
snapshotResolver
[string]
Default:undefined
The path to a module that can resolve test<->snapshot path. This config option lets you customize where Jest stores snapshot files on disk.
module.exports={
// resolves from test to snapshot path
resolveSnapshotPath:(testPath, snapshotExtension)=>
testPath.replace('__tests__','__snapshots__')+ snapshotExtension,
// resolves from snapshot to test path
resolveTestPath:(snapshotFilePath, snapshotExtension)=>
snapshotFilePath
.replace('__snapshots__','__tests__')
.slice(0,-snapshotExtension.length),
// Example test path, used for preflight consistency check of the implementation above
testPathForConsistencyCheck:'some/__tests__/example.test.js',
};
snapshotSerializers
[array<string>]
Default:[]
A list of paths to snapshot serializer modules Jest should use for snapshot testing.
Jest has default serializers for built-in JavaScript types, HTML elements (Jest 20.0.0+), ImmutableJS (Jest 20.0.0+) and for React elements. Seesnapshot test tutorial for more information.
module.exports={
serialize(val, config, indentation, depth, refs, printer){
return`Pretty foo:${printer(val.foo)}`;
},
test(val){
return val&&Object.prototype.hasOwnProperty.call(val,'foo');
},
};
printer
is a function that serializes a value using existing plugins.
Addcustom-serializer
to your Jest configuration:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
snapshotSerializers:['path/to/custom-serializer.js'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
snapshotSerializers:['path/to/custom-serializer.js'],
};
exportdefault config;
Finally tests would look as follows:
test(()=>{
const bar={
foo:{
x:1,
y:2,
},
};
expect(bar).toMatchSnapshot();
});
Rendered snapshot:
Pretty foo: Object{
"x":1,
"y":2,
}
To make a dependency explicit instead of implicit, you can callexpect.addSnapshotSerializer
to add a module for an individual test file instead of adding its path tosnapshotSerializers
in Jest configuration.
More about serializers API can be foundhere.
testEnvironment
[string]
Default:"node"
The test environment that will be used for testing. The default environment in Jest is a Node.js environment. If you are building a web app, you can use a browser-like environment throughjsdom
instead.
By adding a@jest-environment
docblock at the top of the file, you can specify another environment to be used for all tests in that file:
/**
*@jest-environment jsdom
*/
test('use jsdom in this test file',()=>{
const element=document.createElement('div');
expect(element).not.toBeNull();
});
You can create your own module that will be used for setting up the test environment. The module must export a class withsetup
,teardown
andgetVmContext
methods. You can also pass variables from this module to your test suites by assigning them tothis.global
object – this will make them available in your test suites as global variables. The constructor is passedglobalConfig
andprojectConfig
as its first argument, andtestEnvironmentContext
as its second.
The class may optionally expose an asynchronoushandleTestEvent
method to bind to events fired byjest-circus
. Normally,jest-circus
test runner would pause until a promise returned fromhandleTestEvent
gets fulfilled,except for the next events:start_describe_definition
,finish_describe_definition
,add_hook
,add_test
orerror
(for the up-to-date list you can look atSyncEvent type in the types definitions). That is caused by backward compatibility reasons andprocess.on('unhandledRejection', callback)
signature, but that usually should not be a problem for most of the use cases.
Any docblock pragmas in test files will be passed to the environment constructor and can be used for per-test configuration. If the pragma does not have a value, it will be present in the object with its value set to an empty string. If the pragma is not present, it will not be present in the object.
To use this class as your custom environment, refer to it by its full path within the project. For example, if your class is stored inmy-custom-environment.js
in some subfolder of your project, then the annotation might look like this:
/**
*@jest-environment ./src/test/my-custom-environment
*/
TestEnvironment is sandboxed. Each test suite will trigger setup/teardown in their own TestEnvironment.
Example:
// my-custom-environment
constNodeEnvironment=require('jest-environment-node').TestEnvironment;
classCustomEnvironmentextendsNodeEnvironment{
constructor(config, context){
super(config, context);
console.log(config.globalConfig);
console.log(config.projectConfig);
this.testPath= context.testPath;
this.docblockPragmas= context.docblockPragmas;
}
asyncsetup(){
awaitsuper.setup();
awaitsomeSetupTasks(this.testPath);
this.global.someGlobalObject=createGlobalObject();
// Will trigger if docblock contains @my-custom-pragma my-pragma-value
if(this.docblockPragmas['my-custom-pragma']==='my-pragma-value'){
// ...
}
}
asyncteardown(){
this.global.someGlobalObject=destroyGlobalObject();
awaitsomeTeardownTasks();
awaitsuper.teardown();
}
getVmContext(){
returnsuper.getVmContext();
}
asynchandleTestEvent(event, state){
if(event.name==='test_start'){
// ...
}
}
}
module.exports=CustomEnvironment;
// my-test-suite
/**
*@jest-environment ./my-custom-environment
*/
let someGlobalObject;
beforeAll(()=>{
someGlobalObject= globalThis.someGlobalObject;
});
testEnvironmentOptions
[Object]
Default:{}
Test environment options that will be passed to thetestEnvironment
. The relevant options depend on the environment.
For example, you can override options passed tojsdom
:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
testEnvironment:'jsdom',
testEnvironmentOptions:{
html:'<html lang="zh-cmn-Hant"></html>',
url:'https://jestjs.io/',
userAgent:'Agent/007',
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
testEnvironment:'jsdom',
testEnvironmentOptions:{
html:'<html lang="zh-cmn-Hant"></html>',
url:'https://jestjs.io/',
userAgent:'Agent/007',
},
};
exportdefault config;
Bothjest-environment-jsdom
andjest-environment-node
allow specifyingcustomExportConditions
, which allow you to control which versions of a library are loaded fromexports
inpackage.json
.jest-environment-jsdom
defaults to['browser']
.jest-environment-node
defaults to['node', 'node-addons']
.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
testEnvironment:'jsdom',
testEnvironmentOptions:{
customExportConditions:['react-native'],
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
testEnvironment:'jsdom',
testEnvironmentOptions:{
customExportConditions:['react-native'],
},
};
exportdefault config;
These options can also be passed in a docblock, similar totestEnvironment
. The string with options must be parseable byJSON.parse
:
/**
*@jest-environment jsdom
*@jest-environment-options{"url": "https://jestjs.io/"}
*/
test('use jsdom and set the URL in this test file',()=>{
expect(window.location.href).toBe('https://jestjs.io/');
});
testFailureExitCode
[number]
Default:1
The exit code Jest returns on test failure.
This does not change the exit code in the case of Jest errors (e.g. invalid configuration).
testMatch
[array<string>]
(default:[ "**/__tests__/**/*.?([mc])[jt]s?(x)", "**/?(*.)+(spec|test).?([mc])[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
orComponent.spec.js
). It will also find files calledtest.js
orspec.js
.
See themicromatch package for details of the patterns you can specify.
See alsotestRegex
[string | array<string>], but note that you cannot specify both options.
Each glob pattern is applied in the order they are specified in the config. For example["!**/__fixtures__/**", "**/__tests__/**/*.js"]
will not exclude__fixtures__
because 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))\\.[mc]?[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
orComponent.spec.js
). It will also find files calledtest.js
orspec.js
. See alsotestMatch
[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
testRegex
will try to detect test files using theabsolute 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
},
"duration": number |null,
"startAt": epoch |null
},
...
],
"perfStats":{
"end": epoch,
"loadTestEnvironmentEnd": epoch,
"loadTestEnvironmentStart": epoch,
"runtime": number,
"setupAfterEnvEnd": epoch,
"setupAfterEnvStart": epoch,
"setupFilesEnd": epoch,
"setupFilesStart": epoch,
"slow": boolean,
"start": epoch
},
"testFilePath": absolute path to test file,
"coverage":{}
},
"testExecError:" (exists if there was a top-level failure){
"message": string
"stack": string
}
...
]
}
testResultsProcessor
andreporters
are very similar to each other. One difference is that a test result processor only gets called after all tests finished. Whereas a reporter has the ability to receive test results after individual tests and/or test suites are finished.
testRunner
[string]
Default:jest-circus/runner
This option allows the use of a custom test runner. The default isjest-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:
functiontestRunner(
globalConfig: GlobalConfig,
config: ProjectConfig,
environment: Environment,
runtime: Runtime,
testPath:string,
):Promise<TestResult>;
An example of such function can be found in our defaultjasmine2 test runner package.
testSequencer
[string]
Default:@jest/test-sequencer
This option allows you to use a custom sequencer instead of Jest's default.
Bothsort
andshard
may optionally return aPromise
.
For example, you may sort test paths alphabetically:
constSequencer=require('@jest/test-sequencer').default;
classCustomSequencerextendsSequencer{
/**
* Select tests for shard requested via --shard=shardIndex/shardCount
* Sharding is applied before sorting
*/
shard(tests,{shardIndex, shardCount}){
const shardSize=Math.ceil(tests.length/ shardCount);
const shardStart= shardSize*(shardIndex-1);
const shardEnd= shardSize* shardIndex;
return[...tests]
.sort((a, b)=>(a.path> b.path?1:-1))
.slice(shardStart, shardEnd);
}
/**
* Sort test to determine order of execution
* Sorting is applied after sharding
*/
sort(tests){
// Test structure information
// https://github.com/jestjs/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21
const copyTests=[...tests];
return copyTests.sort((testA, testB)=>(testA.path> testB.path?1:-1));
}
}
module.exports=CustomSequencer;
Addcustom-sequencer
to your Jest configuration:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
testSequencer:'path/to/custom-sequencer.js',
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
testSequencer:'path/to/custom-sequencer.js',
};
exportdefault config;
testTimeout
[number]
Default:5000
Default timeout of a test in milliseconds.
transform
[object<string, pathToTransformer | [pathToTransformer, object]>]
Default:{"\\.[jt]sx?$": "babel-jest"}
A map from regular expressions to paths to transformers. Optionally, a tuple with configuration options can be passed as second argument:{filePattern: ['path-to-transformer', {options}]}
. For example, here is how you can configurebabel-jest
for non-default behavior:{'\\.js$': ['babel-jest', {rootMode: 'upward'}]}
.
Jest runs the code of your project as JavaScript, hence a transformer is needed if you use some syntax not supported by Node out of the box (such as JSX, TypeScript, Vue templates). By default, Jest will usebabel-jest
transformer, which will load your project's Babel configuration and transform any file matching the/\.[jt]sx?$/
RegExp (in other words, any.js
,.jsx
,.ts
or.tsx
file). In addition,babel-jest
will inject the Babel plugin necessary for mock hoisting talked about inES Module mocking.
See theCode Transformation section for more details and instructions on building your own transformer.
Keep in mind that a transformer only runs once per file unless the file has changed.
Remember to include the defaultbabel-jest
transformer explicitly, if you wish to use it alongside with additional code preprocessors:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
transform:{
'\\.[jt]sx?$':'babel-jest',
'\\.css$':'some-css-transformer',
},
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
transform:{
'\\.[jt]sx?$':'babel-jest',
'\\.css$':'some-css-transformer',
},
};
exportdefault config;
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 matchesany of the patterns, it will not be transformed.
Providing regexp patterns that overlap with each other may result in files not being transformed that you expected to be transformed. For example:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
transformIgnorePatterns:['/node_modules/(?!(foo|bar)/)','/bar/'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
transformIgnorePatterns:['/node_modules/(?!(foo|bar)/)','/bar/'],
};
exportdefault config;
The first pattern will match (and therefore not transform) files inside/node_modules
except for those in/node_modules/foo/
and/node_modules/bar/
. The second pattern will match (and therefore not transform) files inside any path with/bar/
in it. With the two together, files in/node_modules/bar/
will not be transformed because it does match the second pattern, even though it was excluded by the first.
Sometimes it happens (especially in React Native or TypeScript projects) that 3rd party modules are published as untranspiled code. Since all files insidenode_modules
are not transformed by default, Jest will not understand the code in these modules, resulting in syntax errors. To overcome this, you may usetransformIgnorePatterns
to allow transpiling such modules. You'll find a good example of this use case inReact Native Guide.
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.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
transformIgnorePatterns:[
'<rootDir>/bower_components/',
'<rootDir>/node_modules/',
],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
transformIgnorePatterns:[
'<rootDir>/bower_components/',
'<rootDir>/node_modules/',
],
};
exportdefault config;
If you usepnpm
and need to convert some packages undernode_modules
, you need to note that the packages in this folder (e.g.node_modules/package-a/
) have been symlinked to the path under.pnpm
(e.g.node_modules/.pnpm/[email protected]/node_modules/package-a/
), so using<rootDir>/node_modules/(?!(package-a|@scope/pkg-b)/)
directly will not be recognized, while is to use:
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
transformIgnorePatterns:[
'<rootDir>/node_modules/.pnpm/(?!(package-a|@scope\\+pkg-b)@)',
/* if config file is under '~/packages/lib-a/' */
`${path.join(
__dirname,
'../..',
)}/node_modules/.pnpm/(?!(package-a|@scope\\+pkg-b)@)`,
/* or using relative pattern to match the second 'node_modules/' in 'node_modules/.pnpm/@[email protected]/node_modules/@scope/pkg-b/' */
'node_modules/(?!.pnpm|package-a|@scope/pkg-b)',
],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
transformIgnorePatterns:[
'<rootDir>/node_modules/.pnpm/(?!(package-a|@scope\\+pkg-b)@)',
/* if config file is under '~/packages/lib-a/' */
`${path.join(
__dirname,
'../..',
)}/node_modules/.pnpm/(?!(package-a|@scope\\+pkg-b)@)`,
/* or using relative path to match the second 'node_modules/' in 'node_modules/.pnpm/@[email protected]/node_modules/@scope/pkg-b/' */
'node_modules/(?!.pnpm|package-a|@scope/pkg-b)',
],
};
exportdefault config;
It should be noted that the folder name of pnpm under.pnpm
is the package name plus@
and version number, so writing/
will not be recognized, but using@
can.
unmockedModulePathPatterns
[array<string>]
Default:[]
An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them. If a module's path matches any of the patterns in this list, it will not be automatically mocked by the module loader.
This is useful for some commonly used 'utility' modules that are almost always used as implementation details almost all the time (likeunderscore
,lodash
, etc). It's generally a best practice to keep this list as small as possible and always use explicitjest.mock()
/jest.unmock()
calls in individual tests. Explicit per-test setup is far easier for other readers of the test to reason about the environment the test will run in.
It is possible to override this setting in individual tests by explicitly callingjest.mock()
at the top of the test file.
verbose
[boolean]
Default:false
ortrue
if there is only one test file to run
Indicates whether each individual test should be reported during the run. All errors will also still be shown on the bottom after execution.
waitForUnhandledRejections
[boolean]
Gives one event loop turn to handlerejectionHandled
,uncaughtException
orunhandledRejection
.
Without this flag Jest may report false-positive errors (e.g. actually handled rejection reported) or not report actually unhandled rejection (or report it for different test case).
This option may add a noticeable overhead for fast test suites.
watchPathIgnorePatterns
[array<string>]
Default:[]
An array of RegExp patterns that are matched against all source file paths before re-running tests in watch mode. If the file path matches any of the patterns, when it is updated, it will not trigger a re-run of tests.
These patterns 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>/node_modules/"]
.
Even if nothing is specified here, the watcher will ignore changes to the version control folders (.git, .hg, .sl). Other hidden files and directories, i.e. those that begin with a dot (.
), are watched by default. Remember to escape the dot when you add them towatchPathIgnorePatterns
as it is a special RegExp character.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
watchPathIgnorePatterns:['<rootDir>/\\.tmp/','<rootDir>/bar/'],
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
watchPathIgnorePatterns:['<rootDir>/\\.tmp/','<rootDir>/bar/'],
};
exportdefault config;
watchPlugins
[array<string | [string, Object]>]
Default:[]
This option allows you to use custom watch plugins. Read more about watch pluginshere.
Examples of watch plugins include:
jest-watch-master
jest-watch-select-projects
jest-watch-suspend
jest-watch-typeahead
jest-watch-yarn-workspaces
The values in thewatchPlugins
property value can omit thejest-watch-
prefix of the package name.
watchman
[boolean]
Default:true
Whether to usewatchman
for file crawling.
workerIdleMemoryLimit
[number|string]
Default:undefined
Specifies the memory limit for workers before they are recycled and is primarily a work-around forthis issue;
After the worker has executed a test the memory usage of it is checked. If it exceeds the value specified the worker is killed and restarted. The limit can be specified in a number of different ways and whatever the result isMath.floor
is used to turn it into an integer value:
<= 1
- The value is assumed to be a percentage of system memory. So 0.5 sets the memory limit of the worker to half of the total system memory\> 1
- Assumed to be a fixed byte value. Because of the previous rule if you wanted a value of 1 byte (I don't know why) you could use1.1
.- With units
50%
- As above, a percentage of total system memory100KB
,65MB
, etc - With units to denote a fixed memory limit.K
/KB
- Kilobytes (x1000)KiB
- Kibibytes (x1024)M
/MB
- MegabytesMiB
- MebibytesG
/GB
- GigabytesGiB
- Gibibytes
Percentage based memory limitdoes not work on Linux CircleCI workers due to incorrect system memory being reported.
- JavaScript
- TypeScript
/**@type{import('jest').Config} */
const config={
workerIdleMemoryLimit:0.2,
};
module.exports= config;
importtype{Config}from'jest';
const config: Config={
workerIdleMemoryLimit:0.2,
};
exportdefault config;
//
[string]
This option allows comments inpackage.json
. Include the comment text as the value of this key:
{
"name":"my-project",
"jest":{
"//":"Comment goes here",
"verbose":true
}
}
workerThreads
Default:false
Whether to useworker threads for parallelization.Child processes are used by default.
Using worker threads may help to improveperformance.
This isexperimental feature. Keep in mind that the worker threads use structured clone instead ofJSON.stringify()
to serialize messages. This means that built-in JavaScript objects asBigInt
,Map
orSet
will get serialized properly. However extra properties set onError
,Map
orSet
will not be passed on through the serialization step. For more details see the article onstructured clone.
- Options
- Reference
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>]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