-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
132 lines (123 loc) · 3.48 KB
/
Copy pathjest.setup.js
File metadata and controls
132 lines (123 loc) · 3.48 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require("@testing-library/jest-dom");
const { TextEncoder, TextDecoder } = require("util");
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
if (!global.Headers) {
global.Headers = class Headers {
constructor(init) {
this._map = new Map();
if (init) {
if (init instanceof Map || init instanceof Headers) {
init.forEach((v, k) => this._map.set(k.toLowerCase(), v));
} else {
Object.entries(init).forEach(([k, v]) =>
this._map.set(k.toLowerCase(), v)
);
}
}
}
get(key) {
return this._map.get(key.toLowerCase()) || null;
}
set(key, value) {
this._map.set(key.toLowerCase(), value);
}
has(key) {
return this._map.has(key.toLowerCase());
}
delete(key) {
this._map.delete(key.toLowerCase());
}
append(key, value) {
const existing = this.get(key);
this.set(key, existing ? `${existing}, ${value}` : value);
}
forEach(cb) {
this._map.forEach(cb);
}
entries() {
return this._map.entries();
}
[Symbol.iterator]() {
return this._map.entries();
}
};
}
if (!global.Request) {
global.Request = class Request {
constructor(input, init = {}) {
this.url = typeof input === "string" ? input : input.url;
this.method = init?.method || "GET";
this.headers = init?.headers ? new Headers(init.headers) : new Headers();
this.body = init?.body;
}
async json() {
const text = await this.text();
return JSON.parse(text);
}
async text() {
if (typeof this.body === "string") return this.body;
if (this.body && typeof this.body === "object")
return JSON.stringify(this.body);
return String(this.body || "");
}
};
}
if (!global.Response) {
global.Response = class Response {
constructor(body, init = {}) {
this._body = body;
this.status = init.status || 200;
this.statusText = init.statusText || "OK";
this.headers = new Headers(init.headers);
this.ok = this.status >= 200 && this.status < 300;
}
async json() {
const text = await this.text();
return JSON.parse(text);
}
async text() {
if (typeof this._body === "string") return this._body;
if (this._body instanceof Uint8Array)
return new TextDecoder().decode(this._body);
if (this._body && typeof this._body === "object")
return JSON.stringify(this._body);
return String(this._body || "");
}
};
}
global.__PRISMA_INSTANCES__ = global.__PRISMA_INSTANCES__ || new Set();
afterAll(async () => {
const instances = Array.from(global.__PRISMA_INSTANCES__);
try {
const db = await import("./src/lib/db");
if (db.prisma && !global.__PRISMA_INSTANCES__.has(db.prisma)) {
instances.push(db.prisma);
}
} catch {}
for (const instance of instances) {
if (instance && typeof instance.$disconnect === "function") {
try {
await Promise.race([
instance.$disconnect(),
new Promise((_, reject) =>
setTimeout(() => reject(new Error("Timeout")), 200)
),
]);
} catch {}
}
}
});
Object.defineProperty(window, "matchMedia", {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});