-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
74 lines (66 loc) · 2.92 KB
/
vitest.setup.ts
File metadata and controls
74 lines (66 loc) · 2.92 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import "@testing-library/jest-dom/vitest";
import { expect, vi, beforeEach, afterEach } from "vitest";
import * as matchers from "@testing-library/jest-dom/matchers";
import * as axeMatchers from "vitest-axe/matchers";
import { setProjectAnnotations } from "@storybook/react";
import { TextEncoder, TextDecoder } from "util";
import "vitest-canvas-mock";
import * as projectAnnotations from "./.storybook/preview";
// Extend Vitest's expect with jest-dom matchers
expect.extend(matchers);
// Extend Vitest's expect with axe matchers
expect.extend(axeMatchers);
// Setup Storybook for Vitest
setProjectAnnotations(projectAnnotations);
// Some tests run in a Node.js environment (marked with @vitest-environment node),
// where `window` is not defined. Only set up browser-specific mocks in jsdom.
if (typeof window !== "undefined") {
const { defaultFallbackInView } = require("react-intersection-observer");
const {
setupIntersectionMocking,
resetIntersectionMocking,
} = require("react-intersection-observer/test-utils");
// See https://www.benmvp.com/blog/avoiding-react-act-warning-when-accessibility-testing-next-link-jest-axe/
// If no `IntersectionObserver` exists, Next.js's <Link> will do a state update
// immediately after rendering, causing warnings about wrapping tests in act().
global.IntersectionObserver = vi.fn();
// Then before every test, we add an actual mock for the IntersectionObserver
// API. When a <Link> scrolls into view, Next.js will attempt to preload the
// target, causing another rerender that would cause a warning about wrapping
// tests in act(). Thus, we tell it it's not in view.
defaultFallbackInView(false);
beforeEach(() => {
vi.resetModules();
setupIntersectionMocking(vi.fn);
});
afterEach(() => {
resetIntersectionMocking();
});
// Screen Orientation API mock
// JSDOM often doesn't implement `screen.orientation`, but our Popover component attaches listeners to it
const existingOrientation = globalThis.screen?.orientation;
if (!existingOrientation) {
Object.defineProperty(globalThis.screen, "orientation", {
value: {
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
},
configurable: true,
});
} else {
// If it exists but is incomplete, patch the needed methods.
if (typeof existingOrientation.addEventListener !== "function") {
existingOrientation.addEventListener = vi.fn();
}
if (typeof existingOrientation.removeEventListener !== "function") {
existingOrientation.removeEventListener = vi.fn();
}
}
}
global.TextEncoder = TextEncoder as typeof global.TextEncoder;
if (typeof global.TextDecoder === "undefined") {
global.TextDecoder = TextDecoder as typeof global.TextDecoder;
}