-
Notifications
You must be signed in to change notification settings - Fork 102
/
jest.setup.ts
60 lines (52 loc) · 1.49 KB
/
jest.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
import { MAGIC_BYTE } from './src/wireEncoder'
import decode from './src/wireDecoder'
const toMatchConfluentEncodedPayload = context => (received, { payload: expectedPayload }) => {
const { printExpected, printReceived, printWithType } = context.utils
if (!Buffer.isBuffer(expectedPayload)) {
const error = [
'Expect payload to be a Buffer',
printWithType('Got', expectedPayload, printExpected),
].join('\n')
throw new Error(error)
}
const { magicByte, payload } = decode(received)
const expectedMessage = decode(expectedPayload)
if (!Buffer.isBuffer(received)) {
return {
pass: false,
message: () => [
'Received value must be a Buffer',
printWithType('Received', received, printReceived),
],
}
}
if (Buffer.compare(MAGIC_BYTE, magicByte) !== 0) {
return {
pass: false,
message: () =>
[
'expected magic byte',
printExpected(MAGIC_BYTE),
'\nreceived',
printReceived(magicByte),
].join('\n'),
}
}
return {
pass: context.equals(payload, expectedMessage.payload),
message: () =>
[
'expected payload',
printExpected(expectedMessage.payload),
'\nreceived',
printReceived(payload),
].join('\n'),
}
}
expect.extend({
toMatchConfluentEncodedPayload(...args) {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
return toMatchConfluentEncodedPayload(this)(...args)
},
})