A comprehensive matcher library for Jest testing framework providing assertion utilities for test expectations
npx @tessl/cli install tessl/npm-jest-matchers@20.0.0Jest Matchers is a comprehensive matcher library for the Jest testing framework providing assertion utilities for test expectations. It offers a complete set of assertion methods including equality matchers, comparison operators, collection matchers, spy/mock verifications, and asynchronous testing support with detailed error messages and visual diffs.
npm install jest-matchersconst expect = require('jest-matchers');ES modules:
import expect from 'jest-matchers';const expect = require('jest-matchers');
// Basic equality assertions
expect(2 + 2).toBe(4);
expect({name: 'John'}).toEqual({name: 'John'});
// Truthiness checks
expect(true).toBeTruthy();
expect(null).toBeFalsy();
// Collection assertions
expect(['apple', 'banana']).toContain('apple');
expect([1, 2, 3]).toHaveLength(3);
// Numeric comparisons
expect(10).toBeGreaterThan(5);
expect(3.14).toBeCloseTo(3.1, 1);
// String matching
expect('Hello World').toMatch(/World/);
// Spy/mock verification
const mockFn = jest.fn();
mockFn('arg1', 'arg2');
expect(mockFn).toHaveBeenCalledWith('arg1', 'arg2');
// Exception testing
expect(() => {
throw new Error('Something went wrong');
}).toThrow('Something went wrong');Jest Matchers is built around several core components:
expect.extend().not for inverted assertions.resolves and .rejects for asynchronous testingEssential assertion methods for equality, truthiness, and basic comparisons. These form the foundation of most test assertions.
// Main expect function
function expect(actual: any): ExpectationObject;
// Core equality matchers
ExpectationObject.toBe(expected: any): void;
ExpectationObject.toEqual(expected: any): void;
ExpectationObject.toMatchObject(expected: Object): void;
// Truthiness matchers
ExpectationObject.toBeTruthy(): void;
ExpectationObject.toBeFalsy(): void;
ExpectationObject.toBeDefined(): void;
ExpectationObject.toBeUndefined(): void;
ExpectationObject.toBeNull(): void;Specialized matchers for numeric comparisons and floating-point precision testing.
ExpectationObject.toBeGreaterThan(expected: number): void;
ExpectationObject.toBeGreaterThanOrEqual(expected: number): void;
ExpectationObject.toBeLessThan(expected: number): void;
ExpectationObject.toBeLessThanOrEqual(expected: number): void;
ExpectationObject.toBeCloseTo(expected: number, precision?: number): void;
ExpectationObject.toBeNaN(): void;Matchers for arrays, objects, strings, and other iterable collections.
ExpectationObject.toContain(item: any): void;
ExpectationObject.toContainEqual(item: any): void;
ExpectationObject.toHaveLength(length: number): void;
ExpectationObject.toHaveProperty(path: string, value?: any): void;
ExpectationObject.toMatch(pattern: string | RegExp): void;
ExpectationObject.toBeInstanceOf(constructor: Function): void;Collection and String Matchers
Verification matchers for Jest mock functions and spies, essential for testing function calls and interactions.
ExpectationObject.toBeCalled(): void;
ExpectationObject.toHaveBeenCalled(): void;
ExpectationObject.toBeCalledWith(...args: any[]): void;
ExpectationObject.toHaveBeenCalledWith(...args: any[]): void;
ExpectationObject.toHaveBeenCalledTimes(number: number): void;
ExpectationObject.lastCalledWith(...args: any[]): void;
ExpectationObject.toHaveBeenLastCalledWith(...args: any[]): void;Matchers for testing error conditions and exception handling.
ExpectationObject.toThrow(expected?: string | Error | RegExp): void;
ExpectationObject.toThrowError(expected?: string | Error | RegExp): void;Special matchers for flexible pattern matching and partial object comparison.
expect.anything(): Anything;
expect.any(constructor: Function): Any;
expect.objectContaining(object: Object): ObjectContaining;
expect.arrayContaining(array: Array): ArrayContaining;
expect.stringContaining(string: string): StringContaining;
expect.stringMatching(pattern: string | RegExp): StringMatching;Methods for extending Jest Matchers with custom matchers and configuring test behavior.
expect.extend(matchers: MatchersObject): void;
expect.assertions(expected: number): void;
expect.hasAssertions(): void;
expect.setState(state: Object): void;
expect.getState(): Object;
expect.addSnapshotSerializer(): void;interface ExpectationObject {
// All matchers are available on the expectation object
// Plus special properties for negation and promise handling
not: ExpectationObject;
resolves: ExpectationObject;
rejects: ExpectationObject;
}
interface MatchersObject {
[matcherName: string]: RawMatcherFn;
}
interface ExpectationResult {
pass: boolean;
message?: string | (() => string);
}
class JestAssertionError extends Error {
matcherResult: any;
}