-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjest-bundle-browser-setup.js
59 lines (51 loc) · 1.67 KB
/
jest-bundle-browser-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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {isBuffer} from 'buffer-es6';
import {Buffer as safeBuffer} from 'safe-buffer';
// Convert Buffer implementation of bundled `buffer-es6` to `safe-buffer` implementation used in tests and `safe-buffer` used in rollup's browserify
// It requires to satisfy jest's `.toEqual()` deep equality check
const originalExpect = global.expect;
global.expect = (value) => {
value = walkFix(value);
const expectResult = originalExpect(value);
// fix toEqual
const originalToEqual = expectResult.toEqual;
expectResult.toEqual = (value) => {
value = walkFix(value);
return originalToEqual(value);
};
expectResult.toEqual.__proto__ = originalToEqual;
return expectResult;
};
global.expect.__proto__ = originalExpect;
const WALK_DEPTH = 10;
/**
* walk through keys and fix each
* @param obj
* @param {number} [currentDepth]
*/
function walkFix(obj, currentDepth = 0) {
// fix it if it's a Buffer
obj = fixBuffer(obj);
if (!obj || currentDepth > WALK_DEPTH) {
return obj;
}
// try fix each key if it's an Object
Object.keys(obj).forEach((key) => {
const descriptor = Object.getOwnPropertyDescriptor(obj, key);
if (descriptor.writable) {
obj[key] = walkFix(obj[key], currentDepth + 1);
}
});
return obj;
}
/**
* Convert Buffer implementation of bundled `buffer-es6` to `safe-buffer` implementation used in tests and `safe-buffer` used in rollup's browserify
* It requires to satisfy jest's `.toEqual()` deep equality check
* @param value
* @return {Buffer}
*/
function fixBuffer(value) {
if (isBuffer(value)) {
value = safeBuffer.from(value);
}
return value;
}