-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest-setup.ts
More file actions
106 lines (95 loc) · 3.58 KB
/
Copy pathtest-setup.ts
File metadata and controls
106 lines (95 loc) · 3.58 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
// 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';
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', {
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');
}