Introduction
Jest is a testing framework of Javascript, and Jest CLI is the command-line tool of Jest. It has many options to provide some features while testing. We can use these functions to perform the tests exactly the way we want.
This article will discuss these Jest CLI options. All the available Jest CLI options can be viewed by running “jest --help.”
Jest CLI options
This section will discuss the different available Jest CLI options and what they do.
Jest CLI options with their functionality are listed below:
-
Jest <regexForTestFiles>
When the jest command is run with an argument, that argument is treated as a regular expression. The files in the project that match the pattern will be executed if we run a test suite with a pattern.
-
--bail
This jest CLI option is used to exit the test suite immediately after the first test suite fails.
-
--cache
This CLI option is used to enable cache. We can also disable the cache by the command “--no-cache.”
-
--changedFilesWithAncestor
This CLI option is used for running tests related to the current and last commit changes.
-
--changedSince
This CLI option is used for running tests from the provided branch.
-
--ci
This CLI option is used to make Jest assume that it is running in a CI environment.
-
--clearCache
This CLI option is used to delete the Jest cache and then exit without running the tests.
-
--clearMocks
This CLI option is used to automatically clear mock calls, results and instances before every test.
-
--collectCoverageFrom=<glob>
This CLI option is used to collect coverage information from the file matching to a glob pattern relative to root directory.
-
--config=<path>
This CLI option is used to provide path to a Jest config file that specify how to find and execute tests.
-
--colors
This CLI option is used to force test results output highlighting even if stdout is not a TTY
-
--coverage[=<boolean>]
This CLI option is used to indicate that the test coverage information needs to be calculated and reported in the output.
-
--coverageProvide=<provider>
This CLI option is used for specifying the provider that should be used for instrumenting code for coverage.
-
--debug
This CLI option is used to print debugging information.
-
--detectOpenHandles
This CLI option is used for collecting and printing open handles preventing Jest from exiting cleanly.
-
--env=<environment>
This CLI option is used for specifying the environment for all the tests.
-
--errorOnDeprecated
This CLI option is used to call deprecated APIs through helpful error messages.
-
--expand
This CLI option is used for showing full errors and diffs instead of a patch.
-
--filter=<file>
This CLI option is used to specify path to a module that exports a filtering function.
-
--findRelatedTests <spaceSeparatedListOfSourceFiles>
This CLI option is used for finding and running the tests covering a space separated list of source files passed in as arguments.
-
--forceExit
This CLI option is used to force jest to exit after completing all tests.
-
--help
This CLI command is used to show the help information.
-
--init
This CLI option is used for generating a basic configuration file.
-
--injectGlobals
This CLI option is used for inserting Jest's globals into the global environment.
-
--json
This CLI option is used for printing the output in json format.
-
--lastCommit
This CLI option is used for running all the tests that are affected by the file changes in the last commit made.
-
--listTests
This CLI option is used to list all the tests as JSON that Jest will run and exits.
-
--logHeapUsage
This CLI option is used to see the logs of heap usage after each test.
-
--maxConcurrency=<num>
This CLI option is used to prevent Jest from executing more than the specified amount of tests at the same time.
-
--maxWorkers=<num>|<string>
This CLI option is used for specifying the maximum number of workers the worker-pool will spawn for running tests.
-
--noStackTrace
This CLI option is used to disable the stack trace in test results output.
-
--notify
This CLI option is used to activate notifications for test results.
-
--onlyChanged
This CLI option is used for identifying which tests to run based on which files have changed in the current repository.
-
--outputFile=<filename>
This CLI option is used to write test results to the specified file when the --json option is also specified.
-
--passWithNoTests
This CLI option is used to allow the test suite to pass when no files are found.
-
--projects <path1> ... <pathN>
This CLI option is used for running tests from one or more projects that are found in the specified paths.
-
--reporters
This CLI option is used to run tests with specified reporters.
-
--resetMocks
This CLI option is used for resetting the mock state automatically before every test.
-
--restoreMocks
This CLI option is used for restoring the mock state and implementation automatically before every test.
-
--roots
This CLI option is used for specifying a list of paths to directories that Jest should use to search for files in.
-
--runInBand
This CLI option is used to run all the tests serially in the current process, rather than creating a worker pool of child processes that run the tests.
-
--runTestsByPath
This CLI option is used for running only the tests specified with their exact paths.
-
--selectProjects <project1> ... <projectN>
This CLI option is used for running only the tests of the specified projects.
-
--setupFilesAfterEnv <path1> ... <pathN>
This CLI option is used for specifying a list of paths to modules that run some to set up the testing framework before each test.
-
--showConfig
This CLI option is used to print Jest config and then exit.
-
--silent
This CLI option is used to prevent tests from printing messages through the console.
-
--testEnvironmentOptions=<json string>
This CLI option is used to specify a JSON string with options that will be passed to the test environment.
-
--testLocationInResults
This CLI option is used to specify a JSON string with options that will be passed to the test environment.
-
--testNamePattern=<regex>
This CLI option is used to run the tests that have a name matching with the specified regex.
-
--testRunner=<path>
This CLI option is used to specify a custom test runner.
-
--testSequencer=<path>
This CLI option is used to specify a custom test sequencer.
-
--testTimeout=<number>
This CLI option is used to set the default timeout of a test in milliseconds. The timeout for a test is 5000 milliseconds by default.
-
--updateSnapshot
This CLI option is used for recording those snapshots again that fails during this test run.
-
--useStderr
This CLI option is used for diverting all output to stderr.
-
--verbose
This CLI option is used for displaying test results with test suite hierarchy.
-
--version
This CLI option is used to print the version and then exit.
-
--watch
This CLI option is used to watch changes and rerun tests related to the changed files.
-
--watchAll
This CLI option is used to watch changes and run all the tests.
-
--watchman
This CLI option is used to use watchman for file crawling. We can disable it by using “--no-watchman”.




