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):
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):
Run tests related to
jest --findRelatedTests path/to/fileA.js path/to/fileB.js
Run tests that match this spec name (match against the name in
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 npm scripts #
If you run Jest via
npm test, you can still use the command line arguments by inserting a
npm test and the Jest arguments. Instead of:
jest -u -t="ColorPicker"
you can use:
npm test -- -u -t="ColorPicker"
CLI options take precedence over values from the Configuration.
--projects <project1> ... <projectN>
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. Note: depending on your terminal, you may need to quote this argument:
-b. Exit the test suite immediately upon the first failing test suite.
Whether to use the cache. Defaults to true. Disable the cache using
--no-cache. Note: 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 or clear the cache, use
--showConfig and look at the
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
Relative to the root directory, glob pattern matching the files that coverage info needs to be collected from.
Forces test results output highlighting even if stdout is not a TTY.
-c. The path to a jest config file specifying how to find and execute tests. If no
rootDir is set in the config, the current directory is assumed to be the rootDir for the project. This can also be a JSON-encoded value which Jest will use as configuration.
Indicates that test coverage information should be collected and reported in the output.
Print debugging info about your jest config.
The test environment used for all tests. This can point to any file or node module. Examples:
-e. Use this flag to show full diffs and errors instead of a patch.
--findRelatedTests <spaceSeparatedListOfSourceFiles> #
Find and run the tests that cover a space separated list of source files that were passed in as arguments. Useful for pre-commit hook integration to run the minimal amount of tests necessary.
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. Note: 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.
Show the help information, similar to this page.
Prints the test results in JSON. This mode will send all other test output and user messages to stderr.
Write test results to a file when the
--json option is also specified.
Will run all tests affected by file changes in the last commit made.
Lists all tests as JSON that Jest will run given the arguments, and exits. This can be used together with
--findRelatedTests to know which tests Jest will run.
Logs the heap usage after every test. Useful to debug memory leaks. Use together with
--expose-gc in node.
-w. 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. It may be useful to adjust this in resource limited environments like CIs but the default should be adequate for most use-cases.
Disables stack trace in test results output.
-o. 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/hg repository at the moment and requires a static dependency graph (ie. no dynamic requires).
--projects <project1> ... <projectN> #
Run tests from one or more projects.
-i. Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests. This can be useful for debugging.
The path to a module that runs some code to configure or set up the testing framework before each test. Beware that files imported by the setup script will not be mocked during testing.
Print your Jest config and then exits.
Prevent tests from printing messages through the console.
-t. Run only tests with a name that matches the regex.
A regexp pattern string that is matched against all tests paths before executing the test.
Lets you specify a custom test runner.
-u. Use this flag to re-record every snapshot that fails during this test run. Can be used together with a test suite pattern or with
--testNamePattern to re-record snapshots.
Divert all output to stderr.
Display individual test results with the test suite hierarchy.
-v. Print the version and exit.
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 instead.
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
Whether to use watchman for file crawling. Defaults to true. Disable using