1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-07 00:58:02 +00:00

Fix the custom matcher example

This commit is contained in:
jbranchaud
2020-01-28 12:29:14 -06:00
parent ea4092b769
commit f61df4b73c

View File

@@ -18,12 +18,14 @@ expect.extend({
if (pass) { if (pass) {
return { return {
pass: true, pass: true,
message: `Actual ${recieved.id} matches expected ${expected.id}` message: () =>
`expected id:${expected.id} to not match actual id:${recieved.id}`
}; };
} else { } else {
return { return {
pass: false, pass: false,
message: `Actual ${recieved.id} does not match expected ${expected.id}` message: () =>
`expected id:${expected.id} to match actual id:${recieved.id}`
}; };
} }
} }
@@ -32,14 +34,17 @@ expect.extend({
This defines the name of the matcher (`toHaveMatchingId`), contains some logic This defines the name of the matcher (`toHaveMatchingId`), contains some logic
to figure out whether `received` and `expected` pass the matcher, and then two to figure out whether `received` and `expected` pass the matcher, and then two
return conditions (`pass: true` and `pass: false`) with accompanying messages. return conditions (`pass: true` and `pass: false`) with accompanying
message-returning functions.
It can then be used like any other Jest matcher: It can then be used like any other Jest matcher:
```javascript ```javascript
test("compare objects", () => { test("compare objects", () => {
expect({ id: "001" }).toHaveMatchingId({ id: "001" }); expect({ id: "001" }).toHaveMatchingId({ id: "001" });
// ✅
expect({ id: "001" }).toHaveMatchingId({ id: "002" }); expect({ id: "001" }).toHaveMatchingId({ id: "002" });
// ❌ expected id:002 to match actual id:001
}); });
``` ```