What is the difference between ‘toBe’ and ‘toEqual’ in Jest?

In this article, we’ll look at what is the difference between toBe and toEqual in Jest.

What is the difference between ‘toBe’ and ‘toEqual’ in Jest?

The difference between toBe and toEqual in Jest is that toBe is used for comparing primitive values like numbers, strings, and booleans.

It uses === to do the comparison.

Objects are considered equal with toBe if they reference the same object.

toEqual is used for comparing everything else.

We can use toEqual compare objects directly for equality since their content is checked.

Conclusion

The difference between toBe and toEqual in Jest is that toBe is used for comparing primitive values like numbers, strings, and booleans.