Table of contents
1.
Introduction 
2.
Jest CLI options
3.
Key takeaways-
Last Updated: Mar 27, 2024

Jest CLI Options

Author Riya
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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:

  1. 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.
     
  2. --bail
    This jest CLI option is used to exit the test suite immediately after the first test suite fails.
     
  3. --cache
    This CLI option is used to enable cache. We can also disable the cache by the command “--no-cache.”
     
  4. --changedFilesWithAncestor
    This CLI option is used for running tests related to the current and last commit changes.
     
  5. --changedSince
    This CLI option is used for running tests from the provided branch.
     
  6. --ci
    This CLI option is used to make Jest assume that it is running in a CI environment.
     
  7. --clearCache
    This CLI option is used to delete the Jest cache and then exit without running the tests.
     
  8. --clearMocks
    This CLI option is used to automatically clear mock calls, results and instances before every test.
     
  9. --collectCoverageFrom=<glob>
    This CLI option is used to collect coverage information from the file matching to a glob pattern relative to root directory.
     
  10. --config=<path>
    This CLI option is used to provide path to a Jest config file that specify how to find and execute tests.
     
  11. --colors
    This CLI option is used to force test results output highlighting even if stdout is not a TTY
     
  12.  --coverage[=<boolean>]
    This CLI option is used to indicate that the test coverage information needs to be calculated and reported in the output.
     
  13. --coverageProvide=<provider>
    This CLI option is used for specifying the provider that should be used for instrumenting code for coverage.
     
  14. --debug
    This CLI option is used to print debugging information.
     
  15. --detectOpenHandles
    This CLI option is used for collecting and printing open handles preventing Jest from exiting cleanly.
     
  16. --env=<environment>
    This CLI option is used for specifying the environment for all the tests.
     
  17. --errorOnDeprecated
    This CLI option is used to call deprecated APIs through helpful error messages.
     
  18. --expand
    This CLI option is used for showing full errors and diffs instead of a patch.
     
  19. --filter=<file>
    This CLI option is used to specify path to a module that exports a filtering function.
     
  20. --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. 
     
  21. --forceExit
    This CLI option is used to force jest to exit after completing all tests.
     
  22. --help
    This CLI command is used to show the help information.
     
  23. --init
    This CLI option is used for generating a basic configuration file.
     
  24. --injectGlobals
    This CLI option is used for inserting Jest's globals into the global environment.
     
  25. --json
    This CLI option is used for printing the output in json format.
     
  26. --lastCommit
    This CLI option is used for running all the tests that are affected by the file changes in the last commit made.
     
  27. --listTests
    This CLI option is used to list all the tests as JSON that Jest will run and exits.
     
  28. --logHeapUsage
    This CLI option is used to see the logs of heap usage after each test.
     
  29. --maxConcurrency=<num>
    This CLI option is used  to prevent Jest from executing more than the specified amount of tests at the same time.
     
  30. --maxWorkers=<num>|<string>
    This CLI option is used for specifying the maximum number of workers the worker-pool will spawn for running tests. 
     
  31. --noStackTrace
    This CLI option is used to disable the stack trace in test results output.
     
  32. --notify
    This CLI option is used to activate notifications for test results.
     
  33. --onlyChanged
    This CLI option is used for  identifying which tests to run based on which files have changed in the current repository.
     
  34. --outputFile=<filename>
    This CLI option is used to write test results to the specified file when the --json option is also specified.
     
  35. --passWithNoTests
    This CLI option is used to allow the test suite to pass when no files are found.
     
  36. --projects <path1> ... <pathN>
    This CLI option is used for running  tests from one or more projects that are found in the specified paths.
     
  37. --reporters
    This CLI option is used to run tests with specified reporters.
     
  38. --resetMocks
    This CLI option is used for resetting the mock state automatically before every test.
     
  39. --restoreMocks
    This CLI option is used for restoring the  mock state and implementation automatically  before every test.
     
  40. --roots
    This CLI option is used for specifying a list of paths to directories that Jest should use to search for files in.
     
  41. --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.
     
  42. --runTestsByPath
    This CLI option is used for running only the tests specified with their exact paths.
     
  43. --selectProjects <project1> ... <projectN>
    This CLI option is used for running only the tests of the specified projects.
     
  44. --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.
     
  45. --showConfig
    This CLI option is used to print Jest config and then exit.
     
  46. --silent
    This CLI option is used to prevent tests from printing messages through the console.
     
  47. --testEnvironmentOptions=<json string>
    This CLI option is used to specify a JSON string with options that will be passed to the test environment.
     
  48. --testLocationInResults
    This CLI option is used to specify a JSON string with options that will be passed to the test environment.
     
  49. --testNamePattern=<regex>
    This CLI option is used to run the tests that have a name matching with the specified regex.
     
  50. --testRunner=<path>
    This CLI option is used to specify a custom test runner.
     
  51. --testSequencer=<path>
    This CLI option is used to specify a custom test sequencer.
     
  52. --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.
     
  53. --updateSnapshot
    This CLI option is used for recording those snapshots again that fails during this test run.
     
  54. --useStderr
    This CLI option is used for diverting all output to stderr.
     
  55. --verbose
    This CLI option is used for displaying test results with test suite hierarchy. 
     
  56. --version
    This CLI option is used to print the version and then exit.
     
  57. --watch
    This CLI option is used to watch changes and rerun tests related to the changed files.
     
  58. --watchAll
    This CLI option is used to watch changes and run all the tests.
     
  59. --watchman
    This CLI option is used to use watchman for file crawling. We can disable it by using “--no-watchman”.

 

Key takeaways-

This article discussed what Jest CLI options are and then some of the Jest CLI options with their use cases. If you think that this blog helped you share it with your friends!. 

To be more confident in data structures and algorithms, try out our DS and Algo Course.

Recommended Reading:Ping command in linux

Until then, All the best for your future endeavours, and Keep Coding.

Live masterclass