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
--openHandlesTimeout=<milliseconds>
--outputFile=<filename>
--passWithNoTests
--projects <path1> ... <pathN>
--randomize
--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
--workerThreads
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
.