Matchers in Jest
Last Updated: May 11, 2024
What are Matchers?
Matchers let you test values in different ways. In this block of code, the matcher is .toBe(3)
:
test("Adds 1 + 2 to equal 3", () => {
expect(sum(1, 2)).toBe(3);
});
What do Matchers do?
Matchers are methods to let you validate different things about values. They are used to test the output of a function to determine in a function is working correctly.
Types of Matchers
- toBe( ) - used to test the exact equality of a number, string or boolean.
- toEqual( ) - used when comparing the values of objects or arrays
- toBeFalsy( ) - used to test if a value is
false
,0
,-0
,""
,null
,undefined
, orNaN
. - toBeTruthy( ) - used to match anything an
if
statement treats astrue
. - toThrow( ) - used to test whether a function throws an error.