Matcher
In one line
A matcher is the method that defines how to compare values in an assertion, e.g., toBe for equality, toContain for membership.
In simple words
Matchers are the vocabulary of assertions. When you write expect(value).toBe(5), the toBe part is the matcher. It defines HOW the comparison is done.
Different matchers check different things:
toBe— strict equality (===)toEqual— deep equality (for objects/arrays)toContain— checks if an array/string contains a valuetoBeTruthy/toBeFalsy— checks truthinesstoThrow— checks if a function throwstoMatch— regex matching
Choosing the right matcher is important. Using toBe for objects checks reference equality (usually wrong); using toEqual checks deep equality (usually right).
Example
javascript
expect(5).toBe(5); // strict equality
expect({a: 1}).toEqual({a: 1}); // deep equality
expect('hello').toMatch(/ell/); // regex
expect([1,2,3]).toHaveLength(3); // lengthEach matcher checks the value in a different way.
Related terms
Related Resources
Related Lessons
- Jest Basics
Getting started with Jest.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.