-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
122 lines (117 loc) · 3.85 KB
/
Copy pathvitest.setup.ts
File metadata and controls
122 lines (117 loc) · 3.85 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
// Polyfill Mocha-style globals some legacy compiled tests still reference
// so we can migrate incrementally without editing build artifacts.
import { afterAll, afterEach, beforeAll, beforeEach } from "vitest";
// Only assign if not already defined (Vitest defines the *All variants)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!(globalThis as any).before) (globalThis as any).before = beforeAll;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!(globalThis as any).after) (globalThis as any).after = afterAll;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!(globalThis as any).beforeEach)
(globalThis as any).beforeEach = beforeEach;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!(globalThis as any).afterEach) (globalThis as any).afterEach = afterEach;
// Optional: silence unhandled rejection warnings during migration (can remove later)
process.on("unhandledRejection", (e) => {
// eslint-disable-next-line no-console
console.warn(
"[vitest.setup] Unhandled rejection (test will surface explicit failures separately):",
e,
);
});
// Basic EventTarget polyfill for Node tests needing CustomEvent dispatch/listen
// We only implement the tiny subset used (addEventListener/removeEventListener/dispatchEvent with type + detail)
if (typeof (globalThis as any).addEventListener !== "function") {
const listeners: Record<string, Set<Function>> = {};
(globalThis as any).addEventListener = (type: string, cb: any) => {
(listeners[type] ||= new Set()).add(cb);
};
(globalThis as any).removeEventListener = (type: string, cb: any) => {
listeners[type]?.delete(cb);
};
(globalThis as any).dispatchEvent = (evt: {
type?: string;
detail?: any;
}) => {
const type = (evt as any).type;
if (!type) return true;
for (const cb of listeners[type] || []) {
try {
cb({ detail: (evt as any).detail });
} catch (err) {
// eslint-disable-next-line no-console
console.warn("[vitest.setup] listener error for", type, err);
}
}
return true;
};
// Minimal CustomEvent polyfill if missing
if (typeof (globalThis as any).CustomEvent !== "function") {
(globalThis as any).CustomEvent = class CustomEvent<T = any> {
type: string;
detail: T;
constructor(type: string, init: { detail?: T } = {}) {
this.type = type;
this.detail = init.detail as T;
}
} as any;
}
}
// Optional: debug why a worker won't exit (e.g. active handles / timers)
if (process.env.VITEST_DEBUG_HANDLES) {
afterAll(() => {
const handles = (process as any)._getActiveHandles?.() ?? [];
const requests = (process as any)._getActiveRequests?.() ?? [];
const counts: Record<string, number> = {};
const details: any[] = [];
for (const h of handles) {
const name = h?.constructor?.name ?? typeof h;
counts[name] = (counts[name] ?? 0) + 1;
if (name === "Socket") {
details.push({
type: name,
localAddress: (h as any).localAddress,
localPort: (h as any).localPort,
remoteAddress: (h as any).remoteAddress,
remotePort: (h as any).remotePort,
bytesRead: (h as any).bytesRead,
bytesWritten: (h as any).bytesWritten,
destroyed: (h as any).destroyed,
});
} else if (name === "Pipe") {
details.push({
type: name,
fd: (h as any).fd,
});
}
}
// eslint-disable-next-line no-console
console.log(
"[vitest.setup] debug handles",
counts,
"requests",
requests.length,
"mem",
(process as any).memoryUsage?.(),
"details",
details,
);
// Periodic memory probe (unref so it doesn't keep the worker alive).
try {
const interval = setInterval(() => {
// eslint-disable-next-line no-console
console.log(
"[vitest.setup] mem tick",
(process as any).memoryUsage?.(),
);
}, 10_000);
(interval as any).unref?.();
} catch {
// ignore
}
});
}