@nx/jest - Executors
The @nx/jest plugin provides various executors to help you create and configure jest projects within your Nx workspace. Below is a complete reference for all available executors and their options.
jest
Jest target options for Build Facade.
Jest can be configured in many ways, but primarily you’ll need to at least have the jestConfig options
"test": { "executor": "@nx/jest:jest", "options": { "jestConfig": "libs/my-lib/jest.config.ts" }}
It is also helpful to have passWithNoTests: true
set so your project doesn’t fail testing while tests are still being added.
"test": { "executor": "@nx/jest:jest", "options": { "jestConfig": "libs/my-lib/jest.config.ts", "passWithNoTests": true }}
Snapshots
Update snapshots running with --update-snapshot
or -u
for short.
nx test my-project -u
Other times you might not want to allow updating snapshots such as in CI. Adding a ci configuration is helpful for adding this behavior.
"test": { "executor": "@nx/jest:jest", "options": { "jestConfig": "libs/my-lib/jest.config.ts", "passWithNoTests": true }, "configurations": { "ci": { "ci": true } }}
nx affected --target=test
Learn more about affected
Options
Option | Type | Description | Default |
---|---|---|---|
jestConfig | string [required] | The path of the Jest configuration. (https://jestjs.io/docs/en/configuration) | |
bail | string | Exit the test suite immediately after n number of failing tests. (https://jestjs.io/docs/cli#—bail) | |
changedSince | string | Runs tests related to the changes since the provided branch or commit hash. If the current branch has diverged from the given branch, then only changes made locally will be tested. (https://jestjs.io/docs/cli#—changedsince) | |
ci | boolean | Whether to run Jest in continuous integration (CI) mode. This option is on by default in most popular CI environments. It will prevent snapshots from being written unless explicitly requested. (https://jestjs.io/docs/cli#—ci) | |
clearCache | boolean | Deletes the Jest cache directory and then exits without running tests. Will delete Jest’s default cache directory. Note: clearing the cache will reduce performance. | |
codeCoverage | boolean | Indicates that test coverage information should be collected and reported in the output. (https://jestjs.io/docs/cli#—coverageboolean) | |
color | boolean | Forces test results output color highlighting (even if stdout is not a TTY). Set to false if you would like to have no colors. (https://jestjs.io/docs/cli#—colors) | |
colors | boolean | Forces test results output highlighting even if stdout is not a TTY. (https://jestjs.io/docs/cli#—colors) | |
config | string | The path to a Jest config file specifying how to find and execute tests. If no rootDir is set in the config, the directory containing the config file is assumed to be the rootDir for the project. This can also be a JSON-encoded value which Jest will use as configuration. | |
coverageDirectory | string | The directory where Jest should output its coverage files. | |
coverageReporters | array | A list of reporter names that Jest uses when writing coverage reports. Any istanbul reporter. | |
detectLeaks | boolean | EXPERIMENTAL: Detect memory leaks in tests. After executing a test, it will try to garbage collect the global object used, and fail if it was leaked | |
detectOpenHandles | boolean | Attempt to collect and print open handles preventing Jest from exiting cleanly (https://jestjs.io/docs/cli#—detectopenhandles) | |
findRelatedTests | string | Find and run the tests that cover a comma separated list of source files that were passed in as arguments. (https://jestjs.io/docs/cli#—findrelatedtests-spaceseparatedlistofsourcefiles) | |
forceExit | boolean | Force Jest to exit after all tests have completed running. This is useful when resources set up by test code cannot be adequately cleaned up.This feature is an escape-hatch. If Jest doesn’t exit at the end of a test run, it means external resources are still being held on to or timers are still pending in your code. It is advised to tear down external resources after each test to make sure Jest can shut down cleanly. You can use —detectOpenHandles to help track it down. | |
json | boolean | Prints the test results in JSON . This mode will send all other test output and user messages to stderr . (https://jestjs.io/docs/cli#—json) | |
logHeapUsage | boolean | Logs the heap usage after every test. Useful to debug memory leaks. Use together with —runInBand and —expose-gc in node. | |
maxWorkers | string | Specifies the maximum number of workers the worker-pool will spawn for running tests. This defaults to the number of the cores available on your machine. Useful for CI. (its usually best not to override this default) (https://jestjs.io/docs/cli#—maxworkersnumstring) | |
onlyChanged | boolean | Attempts to identify which tests to run based on which files have changed in the current repository. Only works if you’re running tests in a git or hg repository at the moment. (https://jestjs.io/docs/cli#—onlychanged) | |
outputFile | string | Write test results to a file when the --json option is also specified. (https://jestjs.io/docs/cli#—outputfilefilename) | |
passWithNoTests | boolean | Will not fail if no tests are found (for example while using --testPathPatterns .) (https://jestjs.io/docs/cli#—passwithnotests) | |
randomize | boolean | Shuffle the order of the tests within a file. The shuffling is based on the seed. This option is only supported using the default jest-circus test runner. | |
reporters | array | Run tests with specified reporters. Reporter options are not available via CLI. Example with multiple reporters: jest --reporters="default" --reporters="jest-junit" . (https://jestjs.io/docs/cli#—reporters) | |
runInBand | boolean | Run all tests serially in the current process (rather than creating a worker pool of child processes that run tests). This is sometimes useful for debugging, but such use cases are pretty rare. Useful for CI. (https://jestjs.io/docs/cli#—runinband) | |
setupFile | string | The name of a setup file used by Jest. | |
showConfig | boolean | Print your Jest config and then exits. (https://jestjs.io/docs/en/cli#—showconfig) | |
silent | boolean | Prevent tests from printing messages through the console. (https://jestjs.io/docs/cli#—silent) | |
testFile | string | The name of the file to test. | From command line |
testLocationInResults | boolean | Adds a location field to test results. Used to report location of a test in a reporter. { “column”: 4, “line”: 5 } (https://jestjs.io/docs/cli#—testlocationinresults) | |
testNamePattern | string | Run only tests with a name that matches the regex pattern. (https://jestjs.io/docs/cli#—testnamepatternregex) | |
testPathIgnorePatterns | array | An array of regexp pattern strings that is matched against all tests paths before executing the test. Only run those tests with a path that does not match with the provided regexp expressions. (https://jestjs.io/docs/cli#—testpathignorepatternsregexarray) | |
testPathPatterns | array | An array of regexp pattern strings that is matched against all tests paths before executing the test. (https://jestjs.io/docs/cli#—testpathpatternsregex) | [] |
testResultsProcessor | string | Node module that implements a custom results processor. (https://jestjs.io/docs/en/configuration#testresultsprocessor-string) | |
testTimeout | number | Default timeout of a test in milliseconds. Default value: 5000 . (https://jestjs.io/docs/cli#—testtimeoutnumber) | |
tsConfig | string | The path to the TypeScript configuration file, relative to the workspace root. Note this is only metadata used by Angular migrations. This executor does not use it. | |
updateSnapshot | boolean | Use this flag to re-record snapshots. Can be used together with a test suite pattern or with --testNamePattern to re-record snapshot for test matching the pattern. (https://jestjs.io/docs/cli#—updatesnapshot) | |
useStderr | boolean | Divert all output to stderr. | |
verbose | boolean | Display individual test results with the test suite hierarchy. (https://jestjs.io/docs/cli#—verbose) | |
watch | boolean | Watch files for changes and rerun tests related to changed files. If you want to re-run all tests when a file has changed, use the --watchAll option. (https://jestjs.io/docs/cli#—watch) | |
watchAll | boolean | Watch files for changes and rerun all tests when something changes. If you want to re-run only the tests that depend on the changed files, use the --watch option. (https://jestjs.io/docs/cli#—watchall) |