jest-extended

website·Indexed 33 days ago

https://jest-extended.jestcommunity.dev

A collection of additional matchers for Jest and Vitest, providing extended assertions for types such as arrays, objects, strings, numbers, booleans, dates, and promises. Includes matchers like .toBeArray(), .toBeEmptyObject(), .toBeString(), .toResolve(), and .toHaveBeenCalledBefore().

Tokens
13.3K
Snippets
141
Records
144
Agent score
64%

What's inside jest-extended

  1. Overview of jest-extended matchers

    jest-extended is a library that extends Jest's default assertion APIs by adding more specific matchers, making it easier to write concise and expressive tests for a wider variety of scenarios.
  2. Overview of jest-extended assertion library

    jest-extended is an assertion library that extends Jest's expect function, providing a wider range of matchers for various data types to simplify test writing and improve readability.
  3. Overview of jest-extended

    jest-extended is a library that provides additional custom matchers (assertions) for Jest, extending the default built-in assertion APIs to allow for more specific and convenient testing scenarios.
  4. Selectively import jest-extended matchers in Jest

    Instead of importing all matchers, you can create a custom setup script to import only the specific matchers you need and then add that script to your Jest setupFilesAfterEnv configuration.
    // testSetup.js
    import { toBeArray, toBeSealed } from 'jest-extended';
    expect.extend({ toBeArray, toBeSealed });

    // package.json { "jest": { "setupFilesAfterEnv": ["./testSetup.js"] } }

  5. Setup TypeScript types for jest-extended with Vitest >= 0.31.0

    To enable TypeScript type support for jest-extended matchers in Vitest (version 0.31.0 and newer), create a declaration file (e.g., jest-extended.d.ts) to extend the vitest module interfaces.
    import type CustomMatchers from 'jest-extended';
    import 'vitest';
    
    declare module 'vitest' {
      interface Assertion<T = any> extends CustomMatchers<T> {}
      interface AsymmetricMatchersContaining<T = any> extends CustomMatchers<T> {}
      interface ExpectStatic<T = any> extends CustomMatchers<T> {}
    }
  6. Setup TypeScript types for jest-extended with Vitest < 0.31.0

    For Vitest versions older than 0.31.0, create a declaration file (e.g., jest-extended.d.ts) to extend the vi module interfaces.
    import type CustomMatchers from 'jest-extended';
    import 'vi';
    
    declare module 'vi' {
      interface Assertion<T = any> extends CustomMatchers<T> {}
      interface AsymmetricMatchersContaining<T = any> extends CustomMatchers<T> {}
      interface ExpectStatic<T = any> extends CustomMatchers<T> {}
    }
  7. Install eslint-plugin-jest-extended

    Install eslint and eslint-plugin-jest-extended as development dependencies. If ESLint was installed globally, eslint-plugin-jest-extended must also be installed globally.
    # Using npm
    npm install --save-dev eslint eslint-plugin-jest-extended
    
    # Using Yarn
    yarn add --dev eslint eslint-plugin-jest-extended
  8. Configure jest-extended for Vitest

    Because Vitest's expect.extend API is compatible with Jest, you can use jest-extended by creating a setup script that extends the Vitest expect object and referencing that script in your vitest.config.js.
    // testSetup.js
    import { expect } from 'vitest';
    import * as matchers from 'jest-extended';
    expect.extend(matchers);

    // vitest.config.js export default defineConfig({ test: { setupFiles: ['./testSetup.js'], }, });

  9. Configure eslint-plugin-jest-extended in .eslintrc

    To enable the plugin, add jest-extended to the plugins array in your .eslintrc configuration file (the eslint-plugin- prefix can be omitted). Once added, you can define specific rules under the rules section using the jest-extended/ prefix.
    {
      "plugins": ["jest-extended"],
      "rules": {
        "jest-extended/prefer-to-be-true": "warn",
        "jest-extended/prefer-to-be-false": "error"
      }
    }
  10. Configure jest-extended for Jest

    To use jest-extended with Jest, you must be using Jest version 27.2.5 or newer (for older versions, use jest-extended version 1.2.1). You can enable all matchers by adding jest-extended/all to the setupFilesAfterEnv array in your Jest configuration.
    /** @type {import('jest').Config} */
    const config = {
      setupFilesAfterEnv: ['jest-extended/all'],
    };
  11. Update tsconfig.json for jest-extended with ts-jest >= 25.5.0

    When using ts-jest version 25.5.0 or higher, you must explicitly include the global.d.ts file in the files property of your tsconfig.json to ensure the custom matchers are recognized. Note that adding the files property may change which files are compiled, so you might also need to configure the include property.
    {
      "compilerOptions": {
        // ...
      },
      "files": [
        "global.d.ts"
      ]
    }