-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest-setup.js
42 lines (35 loc) · 1 KB
/
jest-setup.js
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
const fetchMock = require('jest-fetch-mock');
const crypto = require('crypto');
fetchMock.enableMocks();
if (typeof btoa === 'undefined') {
global.btoa = (str) => Buffer.from(str, 'binary').toString('base64');
}
if (typeof atob === 'undefined') {
global.atob = (b64Encoded) => Buffer.from(b64Encoded, 'base64').toString('binary');
}
const error = global.console.error;
global.console.error = (...args) => {
error(...args); // keep default behaviour
throw new Error(...args);
};
global.MessageChannel = jest.fn().mockImplementation(() => {
let onmessage;
return {
port1: {
set onmessage(cb) {
onmessage = cb;
},
},
port2: {
postMessage: (data) => {
onmessage?.({ data });
},
},
};
});
Object.defineProperty(global, 'crypto', {
value: {
getRandomValues: (arr) => crypto.randomBytes(arr.length),
randomUUID: () => crypto.randomBytes(16).toString('hex'),
},
});