Search⌘ K
AI Features

Using Common Matchers

Explore common Jest matchers to write clear and precise assertions in your JavaScript tests. Understand how to check equality, truthiness, function calls, and array content, while improving test readability with negations. This lesson helps you confidently verify expected behaviors in your code using Jest.

What are matchers?

A matcher is what facilitates the assertion portion of a test. It states what we are asserting to be true, ultimately determining the passing or failing of the test.

Jest’s expect function provides a number of these as an out-of-the-box feature. All these are called by simply chaining them onto expect, as shown below:

expect(2 + 2).toEqual(4);

Types of matchers

The matcher library is robust, offering lots of flexibility. Most matchers fall into one of the following categories: ...