jest-extended
repository·main·Indexed 25 days ago
https://github.com/jest-community/jest-extendedA 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.
What's inside jest-extended
- jest-extended is a library that provides additional Jest matchers to extend Jest's default assertion API. It is designed to make testing more convenient by offering more specific matchers for various data types and scenarios beyond what is built into Jest by default.
Explore jest-extended matchers by type
mainjest-extendedprovides 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()
- General:
Install and setup jest-extended
mainTo use
jest-extendedin 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.
Enable jest-extended type declarations in TypeScript
mainTo enable global type support forjest-extendedin a TypeScript project, you must explicitly tell TypeScript where to find the type declarations by adding afilesproperty to yourtsconfig.jsonpointing to thejest-extendedtype index.Build the website for production
mainTo generate the static content for the website, run
yarn build. The resulting static files will be located in thebuilddirectory and can be hosted on any static content hosting service.$ yarn buildConfigure eslint-plugin-jest-extended in ESLint
mainTo enable the plugin, add
jest-extendedto thepluginsarray in your.eslintrcconfiguration file. You can omit theeslint-plugin-prefix. After adding the plugin, you can enable specific rules in therulessection using thejest-extended/prefix.{ "plugins": ["jest-extended"], "rules": { "jest-extended/prefer-to-be-true": "warn", "jest-extended/prefer-to-be-false": "error" } }Install jest-extended via npm or yarn
mainTo use
jest-extendedin your project, install it as a development dependency using either npm or yarn.npm install --save-dev jest-extendedyarn add -D jest-extendedInstall eslint-plugin-jest-extended
mainTo use ESLint rules provided by
jest-extended, you must install botheslintandeslint-plugin-jest-extended.Note: If you have installed ESLint globally, you must also install
eslint-plugin-jest-extendedglobally.npm install --save-dev eslint eslint-plugin-jest-extendedConfigure Vitest TypeScript types for jest-extended
mainTo enable TypeScript type checking for
jest-extendedmatchers in Vitest, you must create a declaration file namedjest-extended.d.ts. The content of this file depends on your Vitest version.For Vitest >= 0.31.0
Use the
vitestmodule 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
vimodule 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 {} }Enable TypeScript type recognition for jest-extended matchers
mainIf your editor does not recognize the custom
jest-extendedmatchers, you need to declare them in a declaration file. Create aglobal.d.tsfile in your project and add the following import:import 'jest-extended';Deploy the website
mainThe website can be deployed using different methods depending on your hosting setup:
- Using SSH: Set the
USE_SSHenvironment variable totrue. - Using GitHub Pages: Set the
GIT_USERenvironment variable to your GitHub username. This will build the site and push it to thegh-pagesbranch.
- Using SSH: Set the
Register jest-extended globally in Jest config
mainTo use
jest-extendedmatchers globally across your Jest tests, you must registerjest-extended/allin your Jest configuration file using thesetupFilesAfterEnvproperty.module.exports = { setupFilesAfterEnv: ['jest-extended/all'], };