Skip to content

Commit 4055cb1

Browse files
committed
Add areErrorsEqual equality tester
- This was done to make sure `toHaveBeenCalledWith` will fail if we pass in the wrong Error constructor. For example the assertion will now fail if we pass in an `Error` instead of a `TypeError`.
1 parent 4709239 commit 4055cb1

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

packages/toolkit/vitest.setup.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ vi.stubGlobal('fetch', nodeFetch)
55
vi.stubGlobal('Request', Request)
66
vi.stubGlobal('Headers', Headers)
77

8+
/**
9+
* Compares two values to determine if they are instances of {@linkcode Error}
10+
* and have the same `name` and `message` properties.
11+
*
12+
* @param a - The first value to compare.
13+
* @param b - The second value to compare.
14+
* @returns `true` if both values are instances of {@linkcode Error} and have the same `name` and `message`.
15+
*/
16+
const areErrorsEqual = (a: unknown, b: unknown) => {
17+
if (a instanceof Error && b instanceof Error) {
18+
return a.name === b.name && a.message === b.message
19+
}
20+
}
21+
22+
expect.addEqualityTesters([areErrorsEqual])
23+
824
beforeAll(() => {
925
server.listen({ onUnhandledRequest: 'error' })
1026
})

0 commit comments

Comments
 (0)