Jest CLI Commands and Options Quiz Quiz

Explore your skills in using Jest command-line interface with this quiz, featuring questions on common commands, options, and their correct usage. Deepen your understanding of jest CLI switches, configuration, and practical scenarios for test execution.

  1. Selecting Test Files with Patterns

    Which Jest CLI option allows you to only run tests that match a specific filename pattern, such as files ending in '.spec.js'?

    1. --testPathPattern
    2. --runFiles
    3. --fileMatch
    4. --grep

    Explanation: The --testPathPattern option lets you specify a regex pattern to match test file paths, which is useful for targeting tests like '.spec.js'. --runFiles is not a recognized Jest CLI option and would not work. --fileMatch sounds plausible but is not a valid Jest command. --grep is not applicable in Jest for file patterns; it's used differently in other test tools.

  2. Showing Individual Test Results

    When running your tests with Jest, which CLI flag ensures that results for each individual test are displayed, rather than a summarized output?

    1. --verbose
    2. --detailed
    3. --list
    4. --print-each

    Explanation: --verbose will display all test results individually, giving more detailed output for test suites and test cases. --detailed and --list are not valid Jest CLI options, although they sound like they might be. --print-each is not an actual Jest flag, so it would not have the intended effect.

  3. Debugging with Node Inspect

    Which sequence would you use to debug Jest tests using Node's built-in inspector from the CLI?

    1. node --inspect-brk ./node_modules/.bin/jest
    2. jest --debug-break
    3. node-debug jest
    4. jest --inspectDebugger

    Explanation: Running node --inspect-brk with the path to the Jest binary starts Jest in debug mode, allowing inspection with developer tools. The flag --debug-break is not recognized by the Jest CLI. node-debug jest is not a recommended or standard approach. --inspectDebugger is not a valid CLI flag.

  4. Testing Changed Files Only

    If you want Jest to only run tests related to files that have been changed since the last commit, which CLI flag should you use?

    1. --onlyChanged
    2. --changedFiles
    3. --updateSnapshot
    4. --since-changed

    Explanation: --onlyChanged instructs Jest to focus on tests related to recently changed files, optimizing test runs in active development. --changedFiles and --since-changed are not valid Jest options, though they sound logical. --updateSnapshot updates snapshot files but does not filter tests by file changes.

  5. Changing Test Environment

    Which Jest CLI option allows you to set a different test environment, such as switching from 'jsdom' to 'node' for tests running in a Node.js context?

    1. --env
    2. --setEnvironment
    3. --context
    4. --environmentNode

    Explanation: The --env option allows you to explicitly specify the test environment, for example setting it to 'node' for backend-style tests. --setEnvironment and --environmentNode are not valid CLI flags in Jest. --context is used for a different purpose and does not change the test environment.