-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjest.polyfills.js
70 lines (59 loc) · 1.73 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
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
// jest.polyfills.js
/**
* to integrate msw 2.x
* ref. https://mswjs.io/docs/faq#requestresponsetextencoder-is-not-defined-jest
*/
const { TextDecoder, TextEncoder } = require('node:util');
const { clearImmediate } = require("node:timers");
Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
clearImmediate: { value: clearImmediate },
});
const { ReadableStream, TransformStream } = require('node:stream/web');
if (globalThis.ReadableStream === undefined) {
globalThis.ReadableStream = ReadableStream
}
if (globalThis.TransformStream === undefined) {
globalThis.TransformStream = TransformStream
}
// Add required web API globals for MSW
const { Blob, File } = require('node:buffer');
const { fetch, Headers, FormData, Request, Response } = require('undici');
// Define these globals for MSW
globalThis.fetch = fetch;
globalThis.Blob = Blob;
globalThis.File = File;
globalThis.Headers = Headers;
globalThis.FormData = FormData;
globalThis.Request = Request;
globalThis.Response = Response;
// MSW v2 requires BroadcastChannel
class BroadcastChannel {
constructor(name) {
this.name = name;
this.listeners = {};
}
postMessage(message) {
// Mock implementation
}
addEventListener(type, listener) {
if (!this.listeners[type]) {
this.listeners[type] = [];
}
this.listeners[type].push(listener);
}
removeEventListener(type, listener) {
if (!this.listeners[type]) {
return;
}
this.listeners[type] = this.listeners[type].filter(l => l !== listener);
}
close() {
this.listeners = {};
}
}
// Add BroadcastChannel to global scope
if (!globalThis.BroadcastChannel) {
globalThis.BroadcastChannel = BroadcastChannel;
}