-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathjest.polyfills.js
More file actions
106 lines (93 loc) · 3.43 KB
/
Copy pathjest.polyfills.js
File metadata and controls
106 lines (93 loc) · 3.43 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
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
98
99
100
101
102
103
104
105
106
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-undef */
const { Buffer } = require("./node_modules/buffer");
// Set Buffer first so undici (and others) can use Buffer.alloc when they load
Object.defineProperty(global, "Buffer", {
value: Buffer,
writable: true,
configurable: true,
});
const { TextDecoder, TextEncoder } = require("node:util");
const { ReadableStream, TransformStream } = require("node:stream/web");
const { MessageChannel, MessagePort } = require("node:worker_threads");
// writable + configurable so they can override read-only globals (e.g. jsdom Window)
Object.defineProperties(global, {
TextDecoder: { value: TextDecoder, writable: true, configurable: true },
TextEncoder: { value: TextEncoder, writable: true, configurable: true },
ReadableStream: { value: ReadableStream },
TransformStream: { value: TransformStream },
MessageChannel: { value: MessageChannel },
MessagePort: { value: MessagePort },
BroadcastChannel: {
value: class {
postMessage() {}
close() {}
onmessage = null;
onmessageerror = null;
},
},
});
const { Blob, File } = require("node:buffer");
const { fetch, Headers, FormData, Request, Response } = require("undici");
const { setTimeout, clearTimeout, setInterval, clearInterval } = global;
const wrapTimer = timerId => {
if (timerId && typeof timerId === "object") {
if (typeof timerId.unref !== "function") {
Object.defineProperties(timerId, {
unref: { value: () => timerId },
ref: { value: () => timerId },
});
}
return timerId;
}
return {
id: timerId,
unref: () => {},
ref: () => {},
};
};
const unwrapTimer = timer =>
timer && typeof timer === "object" && "id" in timer ? timer.id : timer;
// NOTE: Timer wrappers (Buffer already set above for undici)
Object.defineProperties(global, {
setTimeout: { value: (...args) => wrapTimer(setTimeout(...args)) },
clearTimeout: { value: timer => clearTimeout(unwrapTimer(timer)) },
setInterval: { value: (...args) => wrapTimer(setInterval(...args)) },
clearInterval: { value: timer => clearInterval(unwrapTimer(timer)) },
});
// React DOM's scheduler otherwise opens a MessagePort in jsdom-focused tests.
Object.defineProperties(global, {
setImmediate: {
value:
typeof global.setImmediate === "function"
? global.setImmediate
: (callback, ...args) => global.setTimeout(() => callback(...args), 0),
writable: true,
configurable: true,
},
clearImmediate: {
value:
typeof global.clearImmediate === "function"
? global.clearImmediate
: timer => global.clearTimeout(timer),
writable: true,
configurable: true,
},
});
// Polyfill for 'self' (needed for tronweb in Node.js environment)
if (typeof global.self === "undefined") {
global.self = global;
}
// Polyfill for libsodium-wrappers (needed for cosmos-related packages)
if (typeof global.window === "undefined") {
global.window = global;
}
Object.defineProperties(global, {
fetch: { value: fetch, writable: true, configurable: true },
Blob: { value: Blob, writable: true, configurable: true },
File: { value: File, writable: true, configurable: true },
Headers: { value: Headers, writable: true, configurable: true },
FormData: { value: FormData, writable: true, configurable: true },
Request: { value: Request, writable: true, configurable: true },
Response: { value: Response, writable: true, configurable: true },
});