Jest

repository·main·Indexed 12 days ago

https://github.com/jestjs/jest

A comprehensive JavaScript testing solution featuring fast interactive watch mode, snapshot testing, and support for build tools like Babel, webpack, Vite, and Parcel. Includes utilities such as create-jest for project initialization, babel-jest for compilation, and @jest/diff-sequences for comparing sequences.

Tokens
165.6K
Snippets
647
Records
918
Agent score
100%

What's inside Jest

  1. What is jest-haste-map?

    main

    jest-haste-map is a module used by Jest to create a fast lookup of files in a project. It is designed to efficiently locate and track file changes, which is particularly beneficial for large projects.

    Key features include:

    • Parallel crawling and analysis: It crawls the project and extracts dependencies in parallel across worker processes.
    • Cached file system: It maintains a file system cache in memory and on disk for fast module resolution and change detection.
    • Minimal work: It uses watchman (recommended) or @parcel/watcher to detect changes without full file system crawls.
    • File system watching: It can watch the file system, making it suitable for interactive tools like watch mode.
  2. Overview of Jest

    main

    Jest is a JavaScript testing framework designed to be developer-ready and provide instant feedback. Key features include:

    • Out-of-the-box setup: Works immediately for most JavaScript and React projects.
    • Fast feedback loop: Automatically runs failed tests first and includes an interactive mode to switch between running all tests or only those related to changed files.
    • Snapshot Testing: Allows you to capture snapshots of React trees or other serializable values to simplify UI testing and detect unexpected changes.
  3. New features in Jest 27

    main

    Jest 27 introduced several quality-of-life improvements:

    • Interactive Mode: You can now step through failed tests one at a time in interactive mode.
    • Inline Snapshots without Prettier: You can now use inline snapshots in projects that do not use Prettier as a formatter.
    • Asynchronous Transforms: The transform configuration option now supports asynchronous functions, enabling efficient integration with tools like esbuild, Snowpack, and Vite.
    • ESM Support: Improved support for loading custom runners, reporters, and watch plugins as ES modules.
  4. Use @jest/expect as a standalone replacement for expect

    main

    The @jest/expect package provides an extended version of the standard expect library by adding jest-snapshot matchers. It exports a jestExpect object that can be used as a standalone replacement for the default expect function in environments where Jest is not globally available or when you need snapshot testing capabilities in a standalone context.

    import { jestExpect } from '@jest/expect';
    
    // Use jestExpect just like you would use expect
    jestExpect(value).toBe(expected);
  5. New features in Jest 30

    main

    Jest 30 introduces several new capabilities and improvements to the core framework, focusing on performance, ESM support, and developer ergonomics. Key updates include:

    • Improved ESM & TypeScript Support: Enhanced handling for ECMAScript Modules and TypeScript environments.
    • Spies and the using keyword: Support for using the ECMAScript using keyword with spies.
    • expect.arrayOf: A new matcher for validating arrays.
    • test.each placeholder %$: A new placeholder for parameterized tests.
    • jest.advanceTimersToNextFrame(): A new utility for controlling timer progression.
    • Configurable test retries: Ability to configure how many times tests should be retried.
    • jest.unstable_unmockModule(): A new method for unmocking modules.
    • Globals cleanup: Improved cleanup of globals between test files to prevent leakage and improve performance.
  6. Explore the Jest Community ecosystem

    main

    The jest-community GitHub organization hosts high-quality Jest additions, tools, and plugins curated by Jest maintainers and collaborators. These projects are designed to extend Jest's capabilities and improve the testing experience.

    Key community projects include:

    • vscode-jest: Integration for Visual Studio Code.
    • jest-extended: Provides additional matchers for Jest.
    • eslint-plugin-jest: Linting rules for Jest tests.
    • awesome-jest: A curated list of Jest-related resources and projects.
  7. Use the expect function for assertions

    main
    The expect package provides the core expect function used throughout Jest to perform assertions. It is the entry point for all matchers (e.g., toBe, toEqual) used to validate values in your tests. For a full list of available matchers and advanced usage, refer to the official Jest documentation.
  8. Use `JEST_WORKER_ID` to identify worker processes

    main

    Jest assigns a unique, index-based ID to each worker process via the JEST_WORKER_ID environment variable.

    • The IDs start at 1.
    • If you run Jest with the --runInBand CLI flag, all tests will share the same JEST_WORKER_ID of 1 because tests are executed sequentially in a single process.
  9. Develop custom watch plugins with `jest-watch`

    main
    In Jest version 23.1.0, a new package jest-watch was introduced to facilitate the development of custom watch plugins. This package provides the necessary primitives to hook into the Jest watch lifecycle.