-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
23 lines (22 loc) · 1020 Bytes
/
Copy pathvitest.setup.ts
File metadata and controls
23 lines (22 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Patch jsdom's HTMLCanvasElement to use node-canvas for getContext in tests
import { Canvas } from 'canvas';
if (typeof window !== 'undefined' && window.HTMLCanvasElement) {
window.HTMLCanvasElement.prototype.getContext = function getContext(type) {
// @ts-expect-error: patching for test environment
if (!this.__node_canvas_instance) {
// @ts-expect-error: patching for test environment
this.__node_canvas_instance = new Canvas(this.width, this.height);
}
// @ts-expect-error: patching for test environment
return this.__node_canvas_instance.getContext(type);
};
window.HTMLCanvasElement.prototype.toDataURL = function toDataURL() {
// @ts-expect-error: patching for test environment
if (!this.__node_canvas_instance) {
// @ts-expect-error: patching for test environment
this.__node_canvas_instance = new Canvas(this.width, this.height);
}
// @ts-expect-error: patching for test environment
return this.__node_canvas_instance.toDataURL();
};
}