-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest-utils.ts
More file actions
66 lines (56 loc) · 1.7 KB
/
test-utils.ts
File metadata and controls
66 lines (56 loc) · 1.7 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {Blob} from "node:buffer"
import {expect} from "@jest/globals"
import {AsymmetricMatcher} from "expect"
class NumberInRange extends AsymmetricMatcher<number> {
constructor(
private readonly min: number,
private readonly max: number,
) {
super(10)
}
override asymmetricMatch(other: unknown) {
return typeof other === "number" && other >= this.min && other <= this.max
}
override toString(): string {
return `Number between ${this.min} and ${this.max} (inclusive)`
}
override toAsymmetricMatcher() {
return `NumberInRange<${this.min}, ${this.max}>`
}
}
export const numberBetween = (min: number, max: number) =>
new NumberInRange(min, max)
expect.extend({
async toEqualBlob(received: Blob, expected: Blob) {
if (!(received instanceof Blob)) {
return {
pass: false,
message: () =>
"received is not a blob:\n" +
`received: '${this.utils.printReceived(received)}'` +
`expected: '${this.utils.printExpected(expected)}'`,
}
}
const [bufA, bufB] = await Promise.all([
received.arrayBuffer(),
expected.arrayBuffer(),
])
const a = new Uint8Array(bufA)
const b = new Uint8Array(bufB)
const pass = a.length === b.length && a.every((v, i) => v === b[i])
if (pass) {
return {
pass: true,
message: () => "expected blobs not to be equal",
}
} else {
return {
pass: false,
message: () =>
`expected blobs to be equal, but got:\n` +
`received: ${[...a].map((x) => x.toString(16).padStart(2, "0")).join(" ")}\n` +
`expected: ${[...b].map((x) => x.toString(16).padStart(2, "0")).join(" ")}`,
}
}
},
})