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:
toBeusesObject.is— it is reference equality for objects and arrays.expect({a: 1}).toBe({a: 1})fails because the two objects are distinct references.toEqualdoes deep structural comparison — values and structure must match, but class instances and objects with the same shape are treated as equal.undefinedproperties are ignored.toStrictEqualalso deep-compares but additionally checks class types and treatsundefinedproperties differently —{a: undefined}and{}are not equal undertoStrictEqual.
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
toStrictEqualovertoEqualwhen you also want to catchundefinedproperties 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. toMatchObjectchecks 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
toMatchSnapshotandtoMatchInlineSnapshotstore 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,
toContainchecks array membership by reference;toContainEqualdoes a deep equality check, which is what you want for arrays of objects.