-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathhappydom.ts
More file actions
96 lines (92 loc) · 2.81 KB
/
happydom.ts
File metadata and controls
96 lines (92 loc) · 2.81 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
/**
* Happy DOM Setup for Tests
*
* This file is preloaded by Bun before running tests (configured in bunfig.toml).
* It registers Happy DOM's global objects (window, document, HTMLElement, etc.)
* so that tests requiring DOM APIs can run successfully.
*
* @see bunfig.toml - test.preload configuration
* @see https://bun.sh/docs/test/dom
*/
import { GlobalRegistrator } from '@happy-dom/global-registrator';
// Register Happy DOM globals (window, document, etc.)
GlobalRegistrator.register();
// Mock Canvas 2D Context
// Happy DOM doesn't provide canvas rendering APIs, so we mock them for testing.
// This provides enough functionality for terminal tests to run without actual rendering.
const originalGetContext = HTMLCanvasElement.prototype.getContext;
HTMLCanvasElement.prototype.getContext = function (contextType: string, options?: any) {
if (contextType === '2d') {
// Return a minimal mock of CanvasRenderingContext2D
return {
canvas: this,
fillStyle: '#000000',
strokeStyle: '#000000',
font: '12px monospace',
textAlign: 'start',
textBaseline: 'alphabetic',
globalAlpha: 1,
globalCompositeOperation: 'source-over',
imageSmoothingEnabled: true,
lineWidth: 1,
lineCap: 'butt',
lineJoin: 'miter',
miterLimit: 10,
shadowBlur: 0,
shadowColor: 'rgba(0, 0, 0, 0)',
shadowOffsetX: 0,
shadowOffsetY: 0,
// Drawing methods (no-ops for tests)
fillRect: () => {},
strokeRect: () => {},
clearRect: () => {},
fillText: () => {},
strokeText: () => {},
measureText: (text: string) => ({ width: text.length * 8 }),
drawImage: () => {},
save: () => {},
restore: () => {},
scale: () => {},
rotate: () => {},
translate: () => {},
transform: () => {},
setTransform: () => {},
resetTransform: () => {},
createLinearGradient: () => ({
addColorStop: () => {},
}),
createRadialGradient: () => ({
addColorStop: () => {},
}),
createPattern: () => null,
beginPath: () => {},
closePath: () => {},
moveTo: () => {},
lineTo: () => {},
bezierCurveTo: () => {},
quadraticCurveTo: () => {},
arc: () => {},
arcTo: () => {},
ellipse: () => {},
rect: () => {},
fill: () => {},
stroke: () => {},
clip: () => {},
isPointInPath: () => false,
isPointInStroke: () => false,
getTransform: () => ({}),
getImageData: () => ({
data: new Uint8ClampedArray(0),
width: 0,
height: 0,
}),
putImageData: () => {},
createImageData: () => ({
data: new Uint8ClampedArray(0),
width: 0,
height: 0,
}),
} as any;
}
return originalGetContext.call(this, contextType, options);
};