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
Vitest will automatically provide these two values to your equality tester function and expect it to _return a boolean_ indicating whether these two values are equal.
39
39
40
-
But before I get to the comparison part, it's good to have some input validation in place.
41
-
A check that I am actually providing a `Measurement` instance as the `expected` value (i.e. using the equality correctly).
40
+
Measurement comparison only becomes relevant if both the `received` and `expected` values are instances of the `Measurement` class. Let's make sure to check that:
42
41
43
-
```ts file=vitest.setup.ts add=5-10
42
+
```ts file=vitest.setup.ts add=5-7
44
43
import { Measurement } from'./src/measurement'
45
44
46
45
expect.addEqualityTesters([
47
46
function measurementTester(received, expected) {
48
-
if (!(expectedinstanceofMeasurement)) {
49
-
console.log(expected)
50
-
thrownewError(
51
-
'Failed to compare Measurement: expected is not a Measurement',
52
-
)
47
+
if (receivedinstanceofMeasurement&&expectedinstanceofMeasurement) {
48
+
// ...
53
49
}
54
50
},
55
51
])
56
52
```
57
53
58
-
This check exists solely for myself to guard me against mechanical mistakes and typos. I print the invalid `expected` value to the console and throw a message to fail the test.
59
-
60
54
Now, to the equality!
61
55
62
-
If you take a closer look at the `Measurement` class, it already implements an `.equal()` method to help with the comparison.
56
+
If you take a closer look at the implementation of the `Measurement` class, it already has an `.equals()` method to help with the comparison.
For us to use that method, both the `expected` and `received` values have to be an instance of that class. I've already checked that with the `expected` value, and now what remains is to add a similar check for the `received` one:
65
+
So the actual value comparison becomes a matter of invoking that `.equals()` method on any of the measurements provided by the tester:
72
66
73
-
```ts filename=vitest.setup.ts add=12-14
67
+
```ts filename=vitest.setup.ts add=6
74
68
import { Measurement } from'./src/measurement'
75
69
76
70
expect.addEqualityTesters([
77
71
function measurementTester(received, expected) {
78
-
if (!(expectedinstanceofMeasurement)) {
79
-
console.log(expected)
80
-
thrownewError(
81
-
'Failed to compare Measurement: expected is not a Measurement',
82
-
)
83
-
}
84
-
85
-
if (!(receivedinstanceofMeasurement)) {
86
-
returnfalse
72
+
if (receivedinstanceofMeasurement&&expectedinstanceofMeasurement) {
73
+
returnexpected.equals(received)
87
74
}
88
75
},
89
76
])
90
77
```
91
78
92
-
Notice that in this case, if `received` isn't an instance of `Measurement`, I return `false` from the equality tester instead. That is because when this condition met, _the tested value is incorrect_, not my usage of this tester. As a result, I want the equality to fail, which I can do by returning `false` from my tester function at any point.
93
-
94
-
And, finally, let's use the `.equals()` method to compare the two measurements:
95
-
96
-
```ts filename=vitest.setup.ts add=16
97
-
import { Measurement } from'./src/measurement'
98
-
99
-
expect.addEqualityTesters([
100
-
function measurementTester(received, expected) {
101
-
if (!(expectedinstanceofMeasurement)) {
102
-
console.log(expected)
103
-
thrownewError(
104
-
'Failed to compare Measurement: expected is not a Measurement',
105
-
)
106
-
}
107
-
108
-
if (!(receivedinstanceofMeasurement)) {
109
-
returnfalse
110
-
}
111
-
112
-
returnexpected.equals(received)
113
-
},
114
-
])
115
-
```
79
+
> The values you will be comparing might not have the `.equals()` method (or not be classed at all!). Feel free to implement the comparison logic directly in your custom equality tester if that's the case.
116
80
117
-
Once this is done, you don't have to do anything extra for Vitest to respect your custom equality tester. In fact, if you run tests now, you will see them passing, which means Vitest can compare measurements correctly!
81
+
Once this is done, I don't have to do anything extra for Vitest to respect my custom equality tester. In fact, if I run tests now, I will see them passing, which means Vitest can compare measurements correctly!
0 commit comments