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.
website·Indexed 33 days ago
https://jest-extended.jestcommunity.devA 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().
expect function, providing a wider range of matchers for various data types to simplify test writing and improve readability.setupFilesAfterEnv configuration.// testSetup.js
import { toBeArray, toBeSealed } from 'jest-extended';
expect.extend({ toBeArray, toBeSealed });// package.json { "jest": { "setupFilesAfterEnv": ["./testSetup.js"] } }
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> {}
}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> {}
}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-extendedjest-extended package as a development dependency using either npm or yarn to extend Jest with additional matchers.# Using npm
npm install --save-dev jest-extended
# Using yarn
yarn add -D jest-extendedexpect.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'], }, });
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"
}
}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'],
};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"
]
}