-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathjest.polyfills.js
40 lines (37 loc) · 1.18 KB
/
jest.polyfills.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
// jest.polyfills.js
/**
* @note The block below contains polyfills for Node.js globals
* required for Jest to function when running JSDOM tests.
* These HAVE to be require's and HAVE to be in this exact
* order, since "undici" depends on the "TextEncoder" global API.
*
* Consider migrating to a more modern test runner if
* you don't want to deal with this.
*/
import 'web-streams-polyfill/polyfill'
if (typeof global.BroadcastChannel === 'undefined') {
global.BroadcastChannel = class {
constructor(channel) {
this.channel = channel
}
postMessage(message) {}
close() {}
}
}
const {TextDecoder, TextEncoder, ReadableStream} = require('node:util')
Object.defineProperties(globalThis, {
TextDecoder: {value: TextDecoder},
TextEncoder: {value: TextEncoder},
ReadableStream: {value: ReadableStream},
})
const {Blob, File} = require('node:buffer')
const {fetch, Headers, FormData, Request, Response} = require('undici')
Object.defineProperties(globalThis, {
fetch: {value: fetch, writable: true},
Blob: {value: Blob},
File: {value: File},
Headers: {value: Headers},
FormData: {value: FormData},
Request: {value: Request},
Response: {value: Response},
})