-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
vitest.setup.ts
72 lines (67 loc) · 2.19 KB
/
vitest.setup.ts
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
import "@testing-library/jest-dom/vitest";
import { render } from "@ariakit/test/react";
import * as matchers from "@testing-library/jest-dom/matchers";
import { Suspense, createElement, version } from "react";
import failOnConsole from "vitest-fail-on-console";
if (!version.startsWith("17")) {
failOnConsole();
}
expect.extend({
toHaveFocus(element: HTMLElement, expected, options) {
const toHaveFocus = matchers.toHaveFocus.bind(this) as any;
const result = toHaveFocus(element, expected, options);
const { activeElement } = element.ownerDocument;
const activeId = activeElement?.getAttribute("aria-activedescendant");
return {
...result,
pass: result.pass || activeId === element.id,
message: () => {
if (activeId) {
return [
this.utils.matcherHint(
`${this.isNot ? ".not" : ""}.toHaveFocus`,
"element",
"",
),
"",
"Expected:",
` ${this.utils.printExpected(element)}`,
"Received:",
` ${this.utils.printReceived(
element.ownerDocument.getElementById(activeId),
)}`,
].join("\n");
}
return result.message();
},
};
},
});
if (version.startsWith("17")) {
vi.mock("react", async () => {
const actual = await vi.importActual<typeof import("react")>("react");
let id = 0;
const mocks = {
startTransition: (v: () => any) => v(),
useDeferredValue: <T>(v: T) => v,
useTransition: () => [false, (v: () => any) => v()],
useId: () => actual.useMemo(() => `id-${id++}`, []),
};
return { ...mocks, ...actual };
});
}
beforeEach(async ({ task }) => {
const filename = task.file?.name;
if (!filename) return;
const match = filename.match(/examples\/(.*)\/test.ts$/);
if (!match) return;
const [, example] = match;
const { default: comp } = await import(`./examples/${example}/index.tsx`);
const element = createElement(Suspense, {
fallback: null,
// biome-ignore lint/correctness/noChildrenProp:
children: createElement(comp),
});
const { unmount } = await render(element, { strictMode: true });
return unmount;
});