jest-extended

repository·main·Indexed 25 days ago

https://github.com/jest-community/jest-extended

A collection of additional matchers for Jest and Vitest that provide more specific and expressive assertions. Includes matchers for date comparisons (toBeAfter, toBeBefore, toBeBetween), array validation (toBeArray, toBeArrayOfSize), and mutation tracking (toChange, toChangeBy, toChangeTo). Supports global registration via setupFilesAfterEnv and provides an optional ESLint plugin for rule enforcement.

Tokens
13.9K
Snippets
63
Records
83
Agent score
78%

What's inside jest-extended

  1. Explore jest-extended matchers by type

    main

    jest-extended provides a wide range of additional Jest matchers categorized by the data type they validate. You can use these matchers to write more expressive and readable assertions.

    Available matcher categories include:

    • General: .pass(), .fail(), .toBeEmpty(), .toBeOneOf(), .toBeNil(), .toSatisfy()
    • Array: .toBeArray(), .toBeArrayOfSize(), .toIncludeAllMembers(), etc.
    • Boolean: .toBeBoolean(), .toBeTrue(), .toBeFalse()
    • Date: .toBeDate(), .toBeValidDate(), .toBeAfter(), .toBeBetween(), etc.
    • Function: .toBeFunction(), .toChange(), .toThrowWithMessage()
    • Mock: .toHaveBeenCalledBefore(), .toHaveBeenCalledOnce(), etc.
    • Number: .toBeNumber(), .toBeNaN(), .toBeEven(), .toBeWithin(), etc.
    • Object: .toBeObject(), .toContainKey(), .toContainValue(), .toBeFrozen(), etc.
    • Promise: .toResolve(), .toReject()
    • String: .toBeString(), .toBeHexadecimal(), .toEqualCaseInsensitive(), .toStartWith(), etc.
    • Symbol: .toBeSymbol()
  2. Install and setup jest-extended

    main

    To use jest-extended in your project, you need to follow the official installation and setup guides. The project provides additional matchers to Jest's default assertion APIs to make testing more convenient.

    For detailed instructions on how to install the package and configure it within your Jest environment, please refer to the official documentation site.

  3. Configure eslint-plugin-jest-extended in ESLint

    main

    To enable the plugin, add jest-extended to the plugins array in your .eslintrc configuration file. You can omit the eslint-plugin- prefix. After adding the plugin, you can enable specific rules in 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"
      }
    }
  4. Configure Vitest TypeScript types for jest-extended

    main

    To enable TypeScript type checking for jest-extended matchers in Vitest, you must create a declaration file named jest-extended.d.ts. The content of this file depends on your Vitest version.

    For Vitest >= 0.31.0

    Use the vitest module declaration:

    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> {}
    }

    For Vitest < 0.31.0

    Use the vi module declaration:

    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> {}
    }

    You can also extend these declarations with your own custom matchers by adding them to the interface inheritance chain.

    import type CustomMatchers from 'jest-extended';
    import 'vitest';
    
    interface MyCustomMatchers {
      toBeFoo(): any;
    }
    
    declare module 'vitest' {
      interface Assertion<T = any> extends CustomMatchers<T>, MyCustomMatchers {}
      interface AsymmetricMatchersContaining<T = any> extends CustomMatchers<T>, MyCustomMatchers {}
      interface ExpectStatic extends CustomMatchers, MyCustomMatchers {}
    }
  5. Deploy the website

    main

    The website can be deployed using different methods depending on your hosting setup:

    • Using SSH: Set the USE_SSH environment variable to true.
    • Using GitHub Pages: Set the GIT_USER environment variable to your GitHub username. This will build the site and push it to the gh-pages branch.