forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-setup.ts
More file actions
147 lines (136 loc) · 3.92 KB
/
Copy pathtest-setup.ts
File metadata and controls
147 lines (136 loc) · 3.92 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Angular + Zone - Using AnalogJS setup for proper Zone.js integration
import "@angular/compiler";
import "@analogjs/vitest-angular/setup-zone";
import { getTestBed } from "@angular/core/testing";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Injector } from "@angular/core";
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from "@angular/platform-browser-dynamic/testing";
// JSDOM polyfills commonly needed by Angular/CDK/components
// ResizeObserver
if (!(globalThis as any).ResizeObserver) {
class RO {
callback: ResizeObserverCallback;
constructor(cb: ResizeObserverCallback) {
this.callback = cb;
}
observe() {
/* noop */
}
unobserve() {
/* noop */
}
disconnect() {
/* noop */
}
}
(globalThis as any).ResizeObserver = RO as any;
}
// IntersectionObserver
if (!(globalThis as any).IntersectionObserver) {
class IO {
constructor(_: IntersectionObserverCallback) {}
observe() {}
unobserve() {}
disconnect() {}
takeRecords() {
return [];
}
root = null;
rootMargin = "";
thresholds: number[] = [];
}
(globalThis as any).IntersectionObserver = IO as any;
}
// matchMedia
if (!window.matchMedia) {
(window as any).matchMedia = () => ({
matches: false,
media: "",
onchange: null,
addListener: () => {},
removeListener: () => {},
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => false,
});
}
// requestAnimationFrame
if (!globalThis.requestAnimationFrame) {
(globalThis as any).requestAnimationFrame = (cb: FrameRequestCallback) =>
setTimeout(() => cb(Date.now()), 16) as unknown as number;
(globalThis as any).cancelAnimationFrame = (id: number) => clearTimeout(id);
}
// Canvas context - provide a mock implementation for testing
Object.defineProperty(HTMLCanvasElement.prototype, "getContext", {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
value: function (contextType: string) {
// Return mock context for testing
return {
fillRect: () => {},
clearRect: () => {},
getImageData: () => ({ data: [] }),
putImageData: () => {},
createImageData: () => [],
setTransform: () => {},
drawImage: () => {},
save: () => {},
fillText: () => {},
restore: () => {},
beginPath: () => {},
moveTo: () => {},
lineTo: () => {},
closePath: () => {},
stroke: () => {},
translate: () => {},
scale: () => {},
rotate: () => {},
arc: () => {},
fill: () => {},
measureText: () => ({ width: 0 }),
transform: () => {},
rect: () => {},
clip: () => {},
lineWidth: 1,
strokeStyle: "#000",
fillStyle: "#000",
canvas: this,
};
},
writable: true,
configurable: true,
});
// DOMRect
if (!(globalThis as any).DOMRect) {
(globalThis as any).DOMRect = class {
constructor(
public x = 0,
public y = 0,
public width = 0,
public height = 0,
) {}
} as any;
}
// Initialize Angular testing environment once per worker
console.info("[vitest] test-setup.ts running in pid", process.pid);
const testBed = getTestBed();
// Store platform instance globally to reuse across test files
const globalAny = globalThis as any;
if (!globalAny.__ANGULAR_TEST_PLATFORM__) {
console.info("[vitest] Creating Angular test platform");
globalAny.__ANGULAR_TEST_PLATFORM__ = platformBrowserDynamicTesting();
}
// Check if TestBed has already been initialized by checking the platform
if (!testBed.platform) {
console.info("[vitest] Initializing TestBed");
testBed.initTestEnvironment(
BrowserDynamicTestingModule,
globalAny.__ANGULAR_TEST_PLATFORM__,
{ teardown: { destroyAfterEach: false } }, // Don't tear down after each test
);
console.info("[vitest] TestBed initialized");
} else {
console.info("[vitest] TestBed already initialized, skipping");
}