-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
vitest.setup.ts
97 lines (87 loc) · 2.43 KB
/
vitest.setup.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { invariant } from 'outvariant'
import { faker } from '@yellow-ticket/seed-json-schema'
import { normalizeHeaders } from './test/support/inspect'
beforeEach(() => {
faker.seed(1)
})
expect.extend({
toEqualBytes(actual: unknown, expected: unknown) {
invariant(isUint8Array(actual), 'Expected actual to be a Uint8Array')
invariant(isUint8Array(expected), 'Expected expected to be a Uint8Array')
if (actual.length !== expected.length) {
return {
pass: false,
message: () =>
`Expected Uint8Array of length (${expected.length}) but got (${actual.length})`,
actual: actual.length,
expected: expected.length,
}
}
for (let i = 0; i < expected.length; i++) {
if (actual[i] !== expected[i]) {
return {
pass: false,
message: () =>
`Expected Uint8Array to be equal but found a difference at index ${i}`,
actual: actual[i],
expected: expected[i],
}
}
}
return {
pass: true,
message: () => '...',
}
},
async toEqualResponse(actual: Response, expected: Record<string, unknown>) {
invariant(
actual instanceof Response,
'Expected actual to be an instance of Response',
)
invariant(
expected instanceof Response,
'Expected expected to be an instance of Response',
)
// Status code.
if (actual.status !== expected.status) {
return {
pass: false,
message: () => 'Response status codes are not equal',
actual: actual.status,
expected: expected.status,
}
}
// Headers.
if (
!this.equals(
normalizeHeaders(actual.headers),
normalizeHeaders(expected.headers),
)
) {
return {
pass: false,
message: () => 'Response headers are not equal',
actual: Array.from(actual.headers),
expected: Array.from(expected.headers),
}
}
// Body.
const actualBody = await actual.text()
const expectedBody = await expected.text()
if (actualBody !== expectedBody) {
return {
pass: false,
message: () => 'Response bodies are not equal',
actual: actualBody,
expected: expectedBody,
}
}
return {
pass: true,
message: () => 'Responses are equal',
}
},
})
function isUint8Array(value: unknown): value is Uint8Array {
return value?.constructor.name === 'Uint8Array'
}