forked from sininspira2/ResourceTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
59 lines (51 loc) · 1.47 KB
/
jest.setup.js
File metadata and controls
59 lines (51 loc) · 1.47 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
import "@testing-library/jest-dom";
import { TextEncoder, TextDecoder } from "util";
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
// Mock global fetch and related classes
const fetch = require("node-fetch");
global.fetch = fetch;
global.Response = fetch.Response;
global.Headers = fetch.Headers;
// A more complete NextRequest mock
const { Request } = fetch;
class MockNextRequest extends Request {
constructor(input, init) {
super(input, init);
const url = new URL(this.url);
this.nextUrl = {
origin: url.origin,
searchParams: url.searchParams,
};
}
}
global.Request = MockNextRequest;
// Mock NextResponse
jest.mock("next/server", () => {
const { Readable } = require("stream");
const mockNextResponse = function (body, init) {
return {
status: init?.status || 200,
headers: new Map(Object.entries(init?.headers || {})),
body: Readable.from(JSON.stringify(body)),
json: () => Promise.resolve(JSON.parse(body)),
};
};
mockNextResponse.json = (body, init) => {
return {
status: init?.status || 200,
headers: new Map(Object.entries(init?.headers || {})),
body: Readable.from(JSON.stringify(body)),
json: () => Promise.resolve(body),
};
};
return {
NextResponse: mockNextResponse,
NextRequest: MockNextRequest,
};
});