LinuxCommandLibrary

jest

Run JavaScript and TypeScript tests

TLDR

Run all available tests

$ jest
copy

Run the test suites from the given files
$ jest [path/to/file1 path/to/file2 ...]
copy

Run the test suites from files within the current and subdirectories, whose paths match the given regex
$ jest [regex1] [regex2]
copy

Run the tests whose names match the given regex
$ jest --testNamePattern [regex]
copy

Run test suites related to a given source file
$ jest --findRelatedTests [path/to/source_file.js]
copy

Run test suites related to all uncommitted files
$ jest --onlyChanged
copy

Watch files for changes and automatically re-run related tests
$ jest --watch
copy

Display help
$ jest --help
copy

SYNOPSIS

jest [options] [patterns]
jest --version
jest --help

PARAMETERS

--coverage
    Collects code coverage information from your project.

--watch
    Enters a "watch mode," rerunning tests when source files change.

--watchAll
    Similar to --watch, but watches all files, even if not in a Git repo.

--config
    Specifies the path to a Jest configuration file.

--testPathPattern
    Runs tests whose file paths match the provided regular expression.

--runInBand
    Executes all tests serially in the current process, useful for debugging.

--clearCache
    Deletes the Jest cache directory, useful for resolving cache-related issues.

--forceExit
    Forces Jest to exit after test runs complete, even if there are open handles.

--verbose
    Displays individual test results and the full test suite hierarchy.

--silent
    Prevents tests from printing messages to the console during execution.

--version
    Prints the installed Jest version and exits.

--help
    Displays all available CLI options for Jest.

DESCRIPTION

Jest is a widely-adopted JavaScript testing framework developed by Facebook (now Meta) for testing JavaScript and TypeScript codebases. It provides a comprehensive solution for unit, integration, and snapshot testing. Key features include its "zero-config" setup, offering a smooth onboarding experience, alongside exceptional performance through parallel test execution. Jest boasts an integrated assertion library, powerful mocking capabilities for isolating code, and built-in code coverage reporting, simplifying the development workflow. It is commonly utilized in diverse JavaScript environments, including React, Angular, Vue.js, and Node.js applications, significantly contributing to code quality, reliability, and maintainability across projects. Its focus on developer experience makes it a popular choice for modern web development.

CAVEATS

Jest requires Node.js and a package manager like npm or yarn to be installed on your system. It can be resource-intensive for very large test suites, and advanced configurations might require a deeper understanding of its ecosystem. While Jest is versatile, its primary focus remains the JavaScript/TypeScript environment.

SNAPSHOT TESTING

Jest's unique snapshot testing feature is used to test large objects or UI components by comparing a serialized representation (a snapshot) against a previously stored version. This quickly identifies unintended changes to the UI or data structure.

MOCKING

Jest provides powerful mocking capabilities for functions, modules, and timers. This allows developers to isolate the code under test from its dependencies, controlling their behavior and ensuring consistent test results without side effects.

JSDOM

While Jest tests typically run in a Node.js environment, for front-end components that rely on browser APIs, Jest uses JSDOM. JSDOM is a pure JavaScript implementation of web standards, allowing Jest to simulate a browser environment without requiring a real browser.

HISTORY

Jest was developed by Facebook (now Meta) and open-sourced around 2014-2015, initially to test React components. Its innovative features, such as snapshot testing, and a focus on developer experience quickly propelled its adoption. It rapidly became a leading testing framework in the JavaScript ecosystem, continuing to be actively maintained and evolved by Meta and the open-source community.

SEE ALSO

npm(1), yarn(1), node(1), mocha(1), vitest(1), cypress(1)

Copied to clipboard