This repository was archived by the owner on Sep 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathjest.jsdom.setup.js
More file actions
66 lines (56 loc) · 1.62 KB
/
Copy pathjest.jsdom.setup.js
File metadata and controls
66 lines (56 loc) · 1.62 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
// Required for msw
import { rest } from "msw";
import { setupServer } from "msw/node";
import { TextDecoder, TextEncoder } from "util";
// Define missing Jest lifecycle functions if not available
const beforeAll = global.beforeAll || ((fn) => fn());
const afterEach = global.afterEach || ((fn) => fn());
const afterAll = global.afterAll || ((fn) => fn());
// Set up TextEncoder/TextDecoder
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
// Mock BroadcastChannel for JSDOM environment
global.BroadcastChannel = class BroadcastChannel {
constructor() {
this.onmessage = null;
}
postMessage() {}
close() {}
};
// Mock crypto for JSDOM environment
Object.defineProperty(global, "crypto", {
value: {
subtle: {},
getRandomValues: (arr) => {
return require("crypto").randomFillSync(arr);
},
},
});
// Define handlers
const handlers = [
rest.get("*", (req, res, ctx) => {
return res(ctx.json({}));
}),
rest.post("*", (req, res, ctx) => {
return res(ctx.json({}));
}),
rest.put("*", (req, res, ctx) => {
return res(ctx.json({}));
}),
rest.delete("*", (req, res, ctx) => {
return res(ctx.json({}));
}),
rest.patch("*", (req, res, ctx) => {
return res(ctx.json({}));
}),
];
// Create MSW server
const server = setupServer(...handlers);
// Export server to allow adding specific handlers in tests
global.mswServer = server;
// Start server before all tests
beforeAll(() => server.listen({ onUnhandledRequest: "bypass" }));
// Reset handlers after each test
afterEach(() => server.resetHandlers());
// Close server after all tests
afterAll(() => server.close());