- Configuring Jest·续2
- 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]
- preset [string]
- prettierPath [string]
- projects [array<string | ProjectConfig>]
Configuring Jest·续2
Note: While code transformation is applied to the linked setup-file, Jest will not transform any code in node_modules. This is due to the need to load the actual transformers (e.g. babel or typescript) to perform transformation.
Example:
// setup.jsmodule.exports = async () => { // ... // Set reference to mongod in order to close the server during teardown. global.__MONGOD__ = mongod;};
// teardown.jsmodule.exports = async function () { await global.__MONGOD__.stop();};
globalTeardown [string]
Default: undefined
This option allows the use of a custom global teardown module which exports an async function that is triggered once after all test suites. This function gets Jest’s globalConfig object as a parameter.
Note: 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.
Note: The same caveat concerning transformation of node_modules as for globalSetup applies to globalTeardown.
haste [object]
Default: undefined
This will be used to configure the behavior of jest-haste-map, Jest’s internal file crawler/cache system. The following options are supported:
type HasteConfig = { /** 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 on error on module collision. */ throwOnModuleCollision?: boolean; /** Custom HasteMap module */ hasteMapModulePath?: string;};
injectGlobals [boolean]
Default: true
Insert Jest’s globals (expect, test, describe, beforeEach etc.) into the global environment. If you set this to false, 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);});
Note: This option is only supported using the default jest-circus. test runner
maxConcurrency [number]
Default: 5
A number limiting the number of tests that are allowed to run at the same time when using test.concurrent. Any test above this limit will be queued and executed once a slot is released.
maxWorkers [number | string]
设定运行测试的最大工作池数目。 In single run mode, this defaults to the number of the cores available on your machine minus one for the main thread. 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. 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: "maxWorkers": "50%"
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 will override the default, if you wish to still search node_modules for packages include it along with any other options: ["node_modules", "bower_components"]
moduleFileExtensions [array<string>]
Default: ["js", "jsx", "ts", "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 to rootDir value if you want to use file paths.
Additionally, you can substitute captured regex groups using numbered backreferences.
Example:
{ "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" ] }}
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.
Note: If you provide module name without boundaries ^$ it may cause hard to spot errors. E.g. relay will replace all modules which contain relay as a substring in its name: relay, react-relay and graphql-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 be require()-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. Example: ["<rootDir>/build/"].
modulePaths [array<string>]
Default: []
An alternative API to setting the NODE_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. Example: ["<rootDir>/app/"].
notify [boolean]
Default: false
Activates notifications for test results.
Beware: Jest uses node-notifier to display desktop notifications. On Windows, it 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. Requires notify: 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.
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 a jest-preset.json, jest-preset.js, jest-preset.cjs or jest-preset.mjs file at the root.
For example, this preset foo-bar/jest-preset.js will be configured as follows:
{ "preset": "foo-bar"}
Presets may also be relative to filesystem paths.
{ "preset": "./node_modules/foo-bar/jest-preset.js"}
prettierPath [string]
Default: 'prettier'
Sets the path to the prettier node module used to update inline snapshots.
projects [array<string | ProjectConfig>]
Default: undefined
