Using Matchers | Jest
For the full list, see the expect API doc.
Common Matchers#
toBe uses Object.is to test exact equality. If you want to check the value of an object, use toEqual instead:
toEqual recursively checks every field of an object or array.
You can also test for the opposite of a matcher: not.toBe
Truthiness#
toBeNullmatches onlynulltoBeUndefinedmatches onlyundefinedtoBeDefinedis the opposite oftoBeUndefinedtoBeTruthymatches anything that anifstatement treats as truetoBeFalsymatches anything that anifstatement treats as false
Numbers#
For floating point equality, use toBeCloseTo instead of toEqual, because you don't want a test to depend on a tiny rounding error.
Strings#
toMatch: You can check strings against regular expressions
Arrays and iterables#
toContain: You can check if an array or iterable contains a particular item
Exceptions#
toThrow.