Jest CLI Options
The jest
command line runner has a number of useful options. You can run jest --help
to view all available options. Many of the options shown below can also be used together to run tests exactly the way you want. Every one of Jest's Configuration options can also be specified through the CLI.
Here is a brief overview:
Running from the command line
Run all tests (default):
jest
Run only the tests that were specified with a pattern or filename:
jest my-test #or
jest path/to/my-test.js
Run tests related to changed files based on hg/git (uncommitted files):
jest -o
Run tests related to path/to/fileA.js
and path/to/fileB.js
:
jest --findRelatedTests path/to/fileA.js path/to/fileB.js
Run tests that match this spec name (match against the name in describe
or test
, basically).
jest -t name-of-spec
Run watch mode:
jest --watch #runs jest -o by default
jest --watchAll #runs all tests
Watch mode also enables to specify the name or path to a file to focus on a specific set of tests.
Using with package manager
If you run Jest via your package manager, you can still pass the command line arguments directly as Jest arguments.
Instead of:
jest -u -t="ColorPicker"
you can use:
- npm
- Yarn
- pnpm
npm test -- -u -t="ColorPicker"
yarn test -u -t="ColorPicker"
pnpm test -u -t="ColorPicker"
Camelcase & dashed args support
Jest supports both camelcase and dashed arg formats. The following examples will have an equal result:
jest --collect-coverage
jest --collectCoverage
Arguments can also be mixed:
jest --update-snapshot --detectOpenHandles
Options
CLI options take precedence over values from the Configuration.
- Camelcase & dashed args support
- Options
- Reference
jest <regexForTestFiles>
--bail[=<n>]
--cache
--changedFilesWithAncestor
--changedSince
--ci
--clearCache
--clearMocks
--collectCoverageFrom=<glob>
--colors
--config=<path>
--coverage[=<boolean>]
--coverageDirectory=<path>
--coverageProvider=<provider>
--debug
--detectOpenHandles
--env=<environment>
--errorOnDeprecated
--expand
--filter=<file>
--findRelatedTests <spaceSeparatedListOfSourceFiles>
--forceExit
--help
--ignoreProjects <project1> ... <projectN>
--init
--injectGlobals
--json
--lastCommit
--listTests
--logHeapUsage
--maxConcurrency=<num>
--maxWorkers=<num>|<string>
--noStackTrace
--notify
--onlyChanged
--onlyFailures
--outputFile=<filename>
--passWithNoTests
--projects <path1> ... <pathN>
--reporters
--resetMocks
--restoreMocks
--roots
--runInBand
--runTestsByPath
--seed=<num>
--selectProjects <project1> ... <projectN>
--setupFilesAfterEnv <path1> ... <pathN>
--shard
--showConfig
--showSeed
--silent
--testEnvironmentOptions=<json string>
--testLocationInResults
--testMatch glob1 ... globN
--testNamePattern=<regex>
--testPathIgnorePatterns=<regex>|[array]
--testPathPattern=<regex>
--testRunner=<path>
--testSequencer=<path>
--testTimeout=<number>
--updateSnapshot
--useStderr
--verbose
--version
--watch
--watchAll
--watchman
Reference
jest <regexForTestFiles>
When you run jest
with an argument, that argument is treated as a regular expression to match against files in your project. It is possible to run test suites by providing a pattern. Only the files that the pattern matches will be picked up and executed. Depending on your terminal, you may need to quote this argument: jest "my.*(complex)?pattern"
. On Windows, you will need to use /
as a path separator or escape \
as \\
.
--bail[=<n>]
Alias: -b
. Exit the test suite immediately upon n
number of failing test suite. Defaults to 1
.
--cache
Whether to use the cache. Defaults to true. Disable the cache using --no-cache
.
The cache should only be disabled if you are experiencing caching related problems. On average, disabling the cache makes Jest at least two times slower.
If you want to inspect the cache, use --showConfig
and look at the cacheDirectory
value. If you need to clear the cache, use --clearCache
.
--changedFilesWithAncestor
Runs tests related to the current changes and the changes made in the last commit. Behaves similarly to --onlyChanged
.
--changedSince
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. Behaves similarly to --onlyChanged
.
--ci
When this option is provided, Jest will assume it is running in a CI environment. This changes the behavior when a new snapshot is encountered. Instead of the regular behavior of storing a new snapshot automatically, it will fail the test and require Jest to be run with --updateSnapshot
.
--clearCache
Deletes the Jest cache directory and then exits without running tests. Will delete cacheDirectory
if the option is passed, or Jest's default cache directory. The default cache directory can be found by calling jest --showConfig
.
Clearing the cache will reduce performance.
--clearMocks
Automatically clear mock calls, instances, contexts and results before every test. Equivalent to calling jest.clearAllMocks()
before each test. This does not remove any mock implementation that may have been provided.
--collectCoverageFrom=<glob>
A glob pattern relative to rootDir
matching the files that coverage info needs to be collected from.
--colors
Forces test results output highlighting even if stdout is not a TTY.
Alternatively you can set the environment variable FORCE_COLOR=true
to forcefully enable or FORCE_COLOR=false
to disable colorized output. The use of FORCE_COLOR
overrides all other color support checks.
--config=<path>
Alias: -c
. 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.
--coverage[=<boolean>]
Alias: --collectCoverage
. Indicates that test coverage information should be collected and reported in the output. Optionally pass <boolean>
to override option set in configuration.
--coverageDirectory=<path>
The directory where Jest should output its coverage files.
--coverageProvider=<provider>
Indicates which provider should be used to instrument code for coverage. Allowed values are babel
(default) or v8
.
--debug
Print debugging info about your Jest config.
--detectOpenHandles
Attempt to collect and print open handles preventing Jest from exiting cleanly. Use this in cases where you need to use --forceExit
in order for Jest to exit to potentially track down the reason. This implies --runInBand
, making tests run serially. Implemented using async_hooks
. This option has a significant performance penalty and should only be used for debugging.
--env=<environment>
The test environment used for all tests. This can point to any file or node module. Examples: jsdom
, node
or path/to/my-environment.js
.
--errorOnDeprecated
Make calling deprecated APIs throw helpful error messages. Useful for easing the upgrade process.