-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvitest.customMatchers.ts
27 lines (23 loc) · 1022 Bytes
/
vitest.customMatchers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { expect } from 'vitest'
function compressWhitespace(str: string): string {
return str.replace(/\s+/g, ' ').trim()
}
expect.extend({
toEqualIgnoringWhitespace(received: string, expected: string) {
const receivedCompressed = compressWhitespace(received)
const expectedCompressed = compressWhitespace(expected)
const pass = receivedCompressed === expectedCompressed
if (pass) {
return {
message: () =>
`Expected:\n${this.utils.printExpected(expected)}\nReceived:\n${this.utils.printReceived(received)}\n(After compressing whitespace)\nExpected: ${this.utils.printExpected(expectedCompressed)}\nReceived: ${this.utils.printReceived(receivedCompressed)}`,
pass: true,
}
}
return {
message: () =>
`Expected:\n${this.utils.printExpected(expected)}\nReceived:\n${this.utils.printReceived(received)}\n(After compressing whitespace)\nExpected: ${this.utils.printExpected(expectedCompressed)}\nReceived: ${this.utils.printReceived(receivedCompressed)}`,
pass: false,
}
},
})