JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicstestingMatchers and Assertions
PrevNext
testing
beginner
10 min read

Matchers and Assertions

assertions
expect
jest
matchers
toBe
toEqual
toThrow
vitest

The essential Jest and Vitest matchers (toBe, toEqual, toMatch, toContain, toThrow) and how to choose the right assertion for each scenario to write clear, expressive test expectations.

Key Points

1toBe vs toEqual

toBe uses Object.is() for strict reference equality (use for primitives). toEqual performs deep comparison (use for objects and arrays). toStrictEqual adds type checking.

2Floating-Point Precision

Use toBeCloseTo instead of toBe for decimal math to avoid IEEE 754 precision issues. expect(0.1 + 0.2).toBeCloseTo(0.3) passes while toBe fails.

3Exception Testing

toThrow must receive a function that calls the throwing code, not the result of calling it directly. Can match by error message string, regex, or error class.

4Asymmetric Matchers

expect.any(), expect.stringContaining(), and expect.objectContaining() enable flexible matching inside complex structures with dynamic fields like timestamps.

What You'll Learn

  • Choose between toBe, toEqual, and toStrictEqual based on what is being compared
  • Use the right matcher for each data type to produce clear error messages
  • Test thrown exceptions correctly by wrapping the call in a function
  • Apply asymmetric matchers for flexible matching in complex assertions

Deep Dive

Matchers are the building blocks of test assertions. Both Jest and Vitest share the same matcher API (Vitest was designed as a drop-in replacement), so learning these matchers applies to both test runners. Choosing the right matcher makes tests more readable and produces better error messages when tests fail.

Equality Matchers

toBe(value) uses Object.is() for strict equality (similar to ===). Use it for primitives (numbers, strings, booleans) and reference checks. It fails for objects with the same content but different references.

toEqual(value) performs deep equality comparison, recursively checking object properties and array elements. Use it for objects and arrays where you care about the content, not the reference.

JavaScript
expect(2 + 2).toBe(4);                    // primitive: use toBe
expect({ a: 1 }).toEqual({ a: 1 });        // object: use toEqual
expect({ a: 1 }).not.toBe({ a: 1 });       // different references!

toStrictEqual(value) is like toEqual but also checks that objects have the same type (class instance vs plain object) and that there are no undefined properties. Prefer this over toEqual when you want stricter validation.

Truthiness Matchers

  • toBeTruthy(): Passes if the value is truthy (not false, 0, '', null, undefined, NaN)
  • toBeFalsy(): Passes if the value is falsy
  • toBeNull(): Passes only for null
  • toBeUndefined(): Passes only for undefined
  • toBeDefined(): Passes for any value that is not undefined

Use specific matchers (toBeNull, toBeUndefined) over generic ones (toBeFalsy) for clearer intent and better error messages.

Number Matchers

  • toBeGreaterThan(n) / toBeLessThan(n): Strict comparison
  • toBeGreaterThanOrEqual(n) / toBeLessThanOrEqual(n): Inclusive comparison
  • toBeCloseTo(n, digits): Floating-point comparison that avoids precision issues. Use this instead of toBe for decimal math: expect(0.1 + 0.2).toBeCloseTo(0.3)

String Matchers

toMatch(pattern) accepts a regex or string. Use it for partial string matching or pattern validation:

JavaScript
expect('hello world').toMatch(/world/);
expect(errorMessage).toMatch('not found');

Array and Iterable Matchers

  • toContain(item): Checks if an array or string contains the item (uses ===)
  • toContainEqual(item): Like toContain but uses deep equality for objects in arrays
  • toHaveLength(n): Checks the .length property

Object Matchers

  • toHaveProperty(path, value?): Checks nested properties using dot notation or array paths
  • toMatchObject(subset): Checks that the object contains at least the specified properties (allows extra properties)
  • expect.objectContaining({}): Asymmetric matcher for partial object matching inside other matchers

Exception Matchers

toThrow(error?) verifies that a function throws. The function must be wrapped in another function:

JavaScript
expect(() => divide(1, 0)).toThrow('Cannot divide by zero');
expect(() => parseJSON('{')).toThrow(SyntaxError);

Negation

Any matcher can be negated with .not: expect(value).not.toBe(other).

Asymmetric Matchers

For flexible matching inside complex structures:

  • expect.any(Constructor): Matches any instance of the constructor
  • expect.stringContaining(str): Matches strings containing the substring
  • expect.arrayContaining(arr): Matches arrays containing at least those elements

These are powerful for testing objects with dynamic fields like timestamps or IDs.

Fun Fact

Jest's matcher API was inspired by Jasmine, which was itself inspired by Ruby's RSpec. Vitest adopted the same API to be a drop-in replacement, meaning you can often switch from Jest to Vitest by just changing the config file without modifying any test code.

Learn These First

Unit Testing Fundamentals

beginner

Continue Learning

Test Organization and Structure

beginner

Testing Asynchronous Code

beginner

Mocking Functions, Modules, and APIs

intermediate

Practice What You Learned

What are the most commonly used Jest matchers?
junior
jest
Jest matchers are methods used with expect() to test values. Common ones include toBe() for exact equality, toEqual() for deep equality, toBeTruthy()/toBeFalsy() for boolean checks, toContain() for arrays/strings, and toThrow() for errors.
Previous
Integration Tests vs Unit Tests
Next
TDD vs BDD Methodologies
PrevNext