Skip to main content

Setup

caution

jest-extended only supports Jest version 27.2.5 and newer. If you're using an older version of Jest, use 1.2.1.

Add this line to jest.config.mjs

jest.config.mjs
/** @type {import('jest').Config} */
const config = {
///...
setupFilesAfterEnv: ['jest-extended/all'],
///...
};

Or create a setup script with the following:

testSetup.js
// add all jest-extended matchers
import * as matchers from 'jest-extended';
expect.extend(matchers);

// or just add specific matchers
import { toBeArray, toBeSealed } from 'jest-extended';
expect.extend({ toBeArray, toBeSealed });

Add your setup script to your Jest setupFilesAfterEnv configuration. See for help

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

To automatically extend expect with all matchers, you can use

package.json
"jest": {
"setupFilesAfterEnv": ["jest-extended/all"]
}

Use with @jest/globals​

On Jest 28 and newer, importing jest-extended makes the matcher types available on the imported expect.

testSetup.ts
import { expect } from '@jest/globals';
import matchers from 'jest-extended';

expect.extend(matchers);

On Jest 27, use the Jest27Expect type to export an augmented expect from your setup file.

testSetup.ts
import { expect as jestExpect } from '@jest/globals';
import matchers from 'jest-extended';
import type { Jest27Expect } from 'jest-extended';

jestExpect.extend(matchers);

export const expect = jestExpect as Jest27Expect<typeof jestExpect>;

Import expect from that setup file in your Jest 27 tests.

Use with vitest​

For Vitest 5.0.0 and newer, use the dedicated jest-extended/vitest.js entry point in your setup script:

testSetup.ts
import { expect } from 'vitest';
import matchers from 'jest-extended/vitest.js';

expect.extend(matchers);

Add the setup script to your Vitest configuration:

vitest.config.ts
import { defineConfig } from 'vitest/config';

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

Importing this entry point also extends Vitest's matcher types, including .not, .resolves, .rejects, and asymmetric matchers such as expect.toStartWith(). Include your setup script in your TypeScript project. No custom declaration file, Jest types, registration cast, or skipLibCheck setting is required.

This entry point preserves Vitest's native toBeOneOf, toSatisfy, toHaveBeenCalledBefore, and toHaveBeenCalledAfter matchers. It also omits jest-extended's fail matcher to preserve Chai's expect.fail helper. The existing jest-extended entry point continues to export all matchers for Jest.