You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ExpectAPI.md
+189-2Lines changed: 189 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -300,9 +300,9 @@ A string allowing you to display a clear and correct matcher hint:
300
300
-`'resolves'` if matcher was called with the promise `.resolves` modifier
301
301
-`''` if matcher was not called with a promise modifier
302
302
303
-
#### `this.equals(a, b)`
303
+
#### `this.equals(a, b, customTesters?)`
304
304
305
-
This is a deep-equality function that will return `true` if two objects have the same values (recursively).
305
+
This is a deep-equality function that will return `true` if two objects have the same values (recursively). It optionally takes a list of custom equality testers to apply to the deep equality checks (see `this.customTesters` below).
306
306
307
307
#### `this.expand`
308
308
@@ -366,6 +366,10 @@ This will print something like this:
366
366
367
367
When an assertion fails, the error message should give as much signal as necessary to the user so they can resolve their issue quickly. You should craft a precise failure message to make sure users of your custom assertions have a good developer experience.
368
368
369
+
#### `this.customTesters`
370
+
371
+
If your matcher does a deep equality check using `this.equals`, you may want to pass user provided custom testers to `this.equals`. The custom equality testers that the user has provided using the `addEqualityTesters` API are available on this property. The built-in Jest matchers pass `this.customTesters` (along with other built-in testers) to `this.equals` to do deep equality, and your custom matchers may want to do the same.
372
+
369
373
#### Custom snapshot matchers
370
374
371
375
To use snapshot testing inside of your custom matcher you can import `jest-snapshot` and use it from within your matcher.
@@ -495,6 +499,189 @@ it('transitions as expected', () => {
495
499
});
496
500
```
497
501
502
+
### `expect.addEqualityTesters(testers)`
503
+
504
+
You can use `expect.addEqualityTesters` to add your own methods to test if two objects are equal. For example, let's say you have a class in your code that represents volume and it supports determining if two volumes using different units are equal or not. You may want `toEqual` (and other equality matchers) to use this custom equality method when comparing to Volume classes. You can add a custom equality tester to have `toEqual` detect and apply custom logic when comparing Volume classes:
505
+
506
+
```js title="Volume.js"
507
+
// For simplicity in this example, we'll just support the units 'L' and 'mL'
Custom testers are functions that return either the result (`true` or `false`) of comparing the equality of the two given arguments or `undefined` if tester does not handle the given the objects and wants to delegate equality to other testers (for example, the built in equality testers).
620
+
621
+
Custom testers are called with 3 arguments: the two objects to compare and the array of custom testers (used for recursive testers, see section below).
622
+
623
+
These helper functions and properties can be found on `this` inside a custom tester:
624
+
625
+
#### `this.equals(a, b, customTesters?)`
626
+
627
+
This is a deep-equality function that will return `true` if two objects have the same values (recursively). It optionally takes a list of custom equality testers to apply to the deep equality checks. If you use this function, pass through the custom testers your tester is given so further equality checks `equals` applies can also use custom testers the test author may have configured. See the example in the [Recursive custom equality testers][#recursivecustomequalitytesters] section for more details.
628
+
629
+
#### Matchers vs Testers
630
+
631
+
Matchers are methods available on `expect`, for example `expect().toEqual()`. `toEqual` is a matcher. A tester is a method used by matchers that do equality checks to determine if objects are the same.
632
+
633
+
Custom matchers are good to use when you want to provide a custom assertion that test authors can use in their tests. For example, the `toBeWithinRange` example in the [`expect.extend`](#expectextendmatchers) section is a good example of a custom matcher. Sometimes a test author may want to assert two numbers are exactly equal and should use `toBe`. Other times however, a test author may want to allow for some flexibility in their test and `toBeWithinRange` may be a more appropriate assertion.
634
+
635
+
Custom equality testers are good to use for globally extending Jest matchers to apply custom equality logic for all equality comparisons. Test authors can't turn on custom testers for certain assertions and turn off for others (a custom matcher should be used instead if that behavior is desired). For example, defining how to check if two `Volume` objects are equal for all matchers would be a good custom equality tester.
636
+
637
+
#### Recursive custom equality testers
638
+
639
+
If you custom equality testers is testing objects with properties you'd like to do deep equality with, you should use the `this.equals` helper available to equality testers. This `equals` method is the same deep equals method Jest uses internally for all of its deep equality comparisons. Its the method that invokes your custom equality tester. It accepts an array of custom equality testers as a third argument. Custom equality testers are also given an array of custom testers as their third argument. Pass this argument into the third argument of `equals` so that any further equality checks deeper in your object can also take advantage of custom equality testers.
640
+
641
+
For example, let's say you have a `Book` class that contains an array of `Author` classes and both of these classes have custom testers. The `Book` custom tester would want to do a deep equality check on the array of `Author`s and pass in the custom testers given to it so the `Author`s custom equality tester is applied:
642
+
643
+
```js title="customEqualityTesters.js"
644
+
functionareAuthorEqual(a, b) {
645
+
constisAAuthor= a instanceof Author;
646
+
constisBAuthor= b instanceof Author;
647
+
648
+
if (isAAuthor && isBAuthor) {
649
+
// Authors are equal if they have the same name
650
+
returna.name===b.name;
651
+
} elseif (isAAuthor !== isBAuthor) {
652
+
returnfalse;
653
+
} else {
654
+
returnundefined;
655
+
}
656
+
}
657
+
658
+
functionareBooksEqual(a, b, customTesters) {
659
+
constisABook= a instanceof Book;
660
+
constisBBook= b instanceof Book;
661
+
662
+
if (isABook && isBBook) {
663
+
// Books are the same if they have the same name and author array. We need
664
+
// to pass customTesters to equals here so the Author custom tester will be
Remember to define your equality testers as regular functions and **not** arrow functions in order to access the tester context helpers (e.g. `this.equals`).
682
+
683
+
:::
684
+
498
685
### `expect.anything()`
499
686
500
687
`expect.anything()` matches anything but `null` or `undefined`. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a non-null argument:
0 commit comments