-
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
74 lines (62 loc) · 1.52 KB
/
vitest.setup.ts
File metadata and controls
74 lines (62 loc) · 1.52 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
import "@testing-library/jest-dom/vitest";
import { cleanup } from "@testing-library/react";
import { afterAll, afterEach, beforeAll, expect, vi } from "vitest";
import failOnConsole from "vitest-fail-on-console";
const PROTOTYPE_PROPS = [
"clientHeight",
"clientWidth",
"offsetHeight",
"offsetWidth",
];
failOnConsole({
shouldFailOnError: true,
});
expect.addSnapshotSerializer({
serialize(value) {
const rect = value as DOMRect;
return `${rect.x}, ${rect.y} (${rect.width} x ${rect.height})`;
},
test(value) {
return (
value !== null &&
typeof value === "object" &&
"x" in value &&
"y" in value &&
"width" in value &&
"height" in value
);
},
});
expect.extend({
toLogError: (callback: () => unknown, expectedError: string) => {
const spy = vi.spyOn(console, "error").mockImplementation(() => {});
callback();
expect(console.error).toHaveBeenCalledWith(expectedError);
spy.mockReset();
return {
pass: true,
message: () => "",
};
},
});
beforeAll(() => {
PROTOTYPE_PROPS.forEach((propertyKey) => {
Object.defineProperty(HTMLElement.prototype, propertyKey, {
configurable: true,
value: 0,
});
});
vi.spyOn(console, "warn").mockImplementation(() => {
throw Error("Unexpectec console warning");
});
});
afterAll(() => {
PROTOTYPE_PROPS.forEach((propertyKey) => {
delete HTMLElement.prototype[
propertyKey as keyof typeof HTMLElement.prototype
];
});
});
afterEach(() => {
cleanup();
});