Test-driving the pass or failure of an expectation
In this section, you’ll build the basic functionality of the toBeUnprocessableEntity
matcher, ensuring that it will correctly pass or fail your test. But first, we’ll look at the structure of a matcher and then the approach to unit testing matchers.
Understanding matcher structure
Let’s look at the basic structure of a matcher and how we can test it:
export function toTestSomething(received, expected) { const pass = ... const message = () => "..." return { pass, message }; }
The received
parameter value is the object passed to the expect
call. The expected
value is the value passed to the matcher. So, in the example from the introduction, the result is the received object, and the object containing the error
, name
, and dob
properties is the expected object.
The return
value has two important properties...