Jest Matchers Reference

Every Jest expect() matcher with argument type and async variant.

Searchable Jest matcher reference covering equality, truthiness, numbers, strings, arrays, objects, exceptions, mock-function and async matchers with the .not and .resolves/.rejects modifiers. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is the difference between toBe and toEqual?

toBe uses Object.is reference equality, so it suits primitives and the same object reference. toEqual recursively compares structure and value, so it suits arrays and objects. Use toStrictEqual to also check undefined properties and types.

Look up Jest matchers without leaving your editor mindset

Jest’s expect() exposes many matchers, and remembering which to use for references, structure, numbers, exceptions, mocks and promises is half the battle. This reference lists every common matcher, what it asserts, the argument it takes and whether it has an async form, plus the .not, .resolves and .rejects modifiers. It runs entirely in your browser.

How it works

An assertion is expect(value).matcher(expected). The matcher decides the comparison: toBe is Object.is reference equality, toEqual is recursive structural equality, toContain checks membership, and toThrow runs a wrapped function and inspects the error. Modifiers chain before the matcher: .not inverts it, and .resolves/.rejects unwrap a promise:

expect(2 + 2).toBe(4);
expect([1, 2]).toEqual([1, 2]);
expect(() => parse("")).toThrow(/empty/);
await expect(fetchUser(1)).resolves.toHaveProperty("id", 1);
expect(mockFn).toHaveBeenCalledWith("ready");

The critical distinctions: toBe, toEqual, toStrictEqual

These three are the most commonly confused matchers:

  • toBe uses Object.is — it is reference equality for objects and arrays. expect({a: 1}).toBe({a: 1}) fails because the two objects are distinct references.
  • toEqual does deep structural comparison — values and structure must match, but class instances and objects with the same shape are treated as equal. undefined properties are ignored.
  • toStrictEqual also deep-compares but additionally checks class types and treats undefined properties differently — {a: undefined} and {} are not equal under toStrictEqual.

As a practical rule: use toBe for primitives, toEqual for data objects, and toStrictEqual when class types or sparse object shapes matter.

Mock function matchers

When testing behaviour rather than values, mock matchers let you assert how a function was called:

const fn = jest.fn();
fn("hello", 42);

expect(fn).toHaveBeenCalled();
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).toHaveBeenCalledWith("hello", 42);
expect(fn).toHaveBeenLastCalledWith("hello", 42);
expect(fn).toHaveBeenNthCalledWith(1, "hello", 42);

expect.any(Constructor) is useful as a placeholder in toHaveBeenCalledWith when you care about the type but not the exact value: expect(fn).toHaveBeenCalledWith(expect.any(String), expect.any(Number)).

Tips and examples

  • Prefer toStrictEqual over toEqual when you also want to catch undefined properties and mismatched class types.
  • Use expect.assertions(n) at the top of async tests to guarantee a callback assertion actually ran — critical for tests with conditional paths that may exit without asserting.
  • toMatchObject checks that an object contains a subset of expected properties, ignoring extras — handy for API responses where you care about specific fields but not the whole body.
  • Snapshot matchers toMatchSnapshot and toMatchInlineSnapshot store the rendered value on first run and compare on later runs. Inline snapshots live in the test file itself, making small components easy to review.
  • For collections, toContain checks array membership by reference; toContainEqual does a deep equality check, which is what you want for arrays of objects.