Define A Custom Jest Matcher
Though Jest's built-in matchers will get you pretty far in most testing scenarios, you may find yourself in a testing situation better served by a custom matcher. Custom matchers can be defined using the expect.extend()
function.
Here is an example of a matcher that can check equality of two objects based solely on their id
property.
This defines the name of the matcher (toHaveMatchingId
), contains some logic to figure out whether received
and expected
pass the matcher, and then two return conditions (pass: true
and pass: false
) with accompanying message-returning functions.
It can then be used like any other Jest matcher:
Check out a live example.
PreviousDefault And Named Exports From The Same ModuleNextDestructure With Access To Nested Value And Parent Value
Last updated