Skip to content
TypeScript

test(web): burst-merged jsdom tests run in a real browser (#21318)

Julius Marminge8cd9641f7687
diff --git a/apps/web/src/components/CommandPalette.focus.test.tsx b/apps/web/src/components/CommandPalette.focus.browser.test.tsx
similarity index 96%
rename from apps/web/src/components/CommandPalette.focus.test.tsx
rename to apps/web/src/components/CommandPalette.focus.browser.test.tsx
index 641a79c328a..66d8795741a 100644
--- a/apps/web/src/components/CommandPalette.focus.test.tsx
+++ b/apps/web/src/components/CommandPalette.focus.browser.test.tsx
@@ -1,5 +1,3 @@
-// @vitest-environment jsdom
-
import { act, useImperativeHandle, useRef } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
@@ -73,18 +71,6 @@ let container: HTMLDivElement;
beforeEach(() => {
vi.useFakeTimers();
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
- vi.stubGlobal(
- "ResizeObserver",
- class {
- observe() {}
- disconnect() {}
- unobserve() {}
- },
- );
- Object.defineProperty(Element.prototype, "getAnimations", {
- configurable: true,
- value: () => [],
- });
container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.input.test.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx
similarity index 97%
rename from apps/web/src/components/ComposerPromptEditorTiptap.input.test.tsx
rename to apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx
index d4f82a829fb..8af19e152c7 100644
--- a/apps/web/src/components/ComposerPromptEditorTiptap.input.test.tsx
+++ b/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx
@@ -1,5 +1,3 @@
-// @vitest-environment jsdom
-
import type { Editor } from "@tiptap/core";
import { act, createRef } from "react";
import { createRoot, type Root } from "react-dom/client";
@@ -43,7 +41,6 @@ async function mountEditor(value: string, richTextEnabled = true) {
);
const element = container.querySelector<HTMLElement & { editor: Editor }>(".tiptap");
if (!element) throw new Error("Composer did not mount");
- element.editor.view.setProps({ handleScrollToSelection: () => true });
return element.editor;
}
diff --git a/apps/web/src/components/chat/MermaidDiagram.test.tsx b/apps/web/src/components/chat/MermaidDiagram.browser.test.tsx
similarity index 99%
rename from apps/web/src/components/chat/MermaidDiagram.test.tsx
rename to apps/web/src/components/chat/MermaidDiagram.browser.test.tsx
index b088a6eae38..7d65c480da5 100644
--- a/apps/web/src/components/chat/MermaidDiagram.test.tsx
+++ b/apps/web/src/components/chat/MermaidDiagram.browser.test.tsx
@@ -1,5 +1,3 @@
-// @vitest-environment jsdom
-
import { describe, expect, it } from "vite-plus/test";
import { sanitizeMermaidSvg } from "./MermaidDiagram";
diff --git a/apps/web/src/components/chat/threadContextDrag.test.tsx b/apps/web/src/components/chat/threadContextDrag.browser.test.tsx
similarity index 98%
rename from apps/web/src/components/chat/threadContextDrag.test.tsx
rename to apps/web/src/components/chat/threadContextDrag.browser.test.tsx
index 1ad421d030e..127560eb92f 100644
--- a/apps/web/src/components/chat/threadContextDrag.test.tsx
+++ b/apps/web/src/components/chat/threadContextDrag.browser.test.tsx
@@ -1,5 +1,3 @@
-// @vitest-environment jsdom
-
import { act, memo, useRef } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
diff --git a/apps/web/src/components/preview/PreviewPanelShell.a11y.test.tsx b/apps/web/src/components/preview/PreviewPanelShell.a11y.browser.test.tsx
similarity index 88%
rename from apps/web/src/components/preview/PreviewPanelShell.a11y.test.tsx
rename to apps/web/src/components/preview/PreviewPanelShell.a11y.browser.test.tsx
index d4f8f0aaae0..bf6874368f1 100644
--- a/apps/web/src/components/preview/PreviewPanelShell.a11y.test.tsx
+++ b/apps/web/src/components/preview/PreviewPanelShell.a11y.browser.test.tsx
@@ -1,9 +1,8 @@
-// @vitest-environment jsdom
-
import { Dialog } from "@base-ui/react/dialog";
import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
+import { userEvent } from "vite-plus/test/browser";
import { PanelLayoutControls } from "../chat/PanelLayoutControls";
import { PreviewPanelShell, type PreviewPanelMode } from "./PreviewPanelShell";
@@ -13,15 +12,6 @@ let container: HTMLDivElement;
beforeEach(() => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
- vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame"] });
- vi.stubGlobal(
- "ResizeObserver",
- class {
- observe() {}
- unobserve() {}
- disconnect() {}
- },
- );
container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
@@ -31,7 +21,6 @@ afterEach(async () => {
await act(async () => root.unmount());
container.remove();
vi.unstubAllGlobals();
- vi.useRealTimers();
});
async function renderPanel(
@@ -76,17 +65,20 @@ async function renderPanel(
</Dialog.Root>,
);
});
- await act(async () => vi.advanceTimersToNextFrame());
+ await act(nextFrame);
+}
+
+function nextFrame() {
+ return new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
}
+// Tabbing past the popup's last control wraps through Base UI's focus guard
+// to the first tabbable descendant, which skips anything inert.
async function enterFocusScope() {
- // Base UI's focus guard models Tab wrapping into the scope. Its production
- // focus manager chooses the first tabbable descendant, including inert rules
- // that jsdom's native focus() does not implement.
- const guards = document.querySelectorAll<HTMLElement>("[data-base-ui-focus-guard]");
- expect(guards.length).toBe(2);
- await act(async () => guards[1]!.focus());
- await act(async () => vi.advanceTimersToNextFrame());
+ const toggle = document.querySelector<HTMLElement>('[aria-label="Toggle right panel"]')!;
+ await act(async () => toggle.focus());
+ await act(() => userEvent.tab());
+ await act(nextFrame);
}
describe("right panel keyboard access", () => {
diff --git a/apps/web/src/components/pullRequest/PullRequestCandidatePicker.test.tsx b/apps/web/src/components/pullRequest/PullRequestCandidatePicker.browser.test.tsx
similarity index 67%
rename from apps/web/src/components/pullRequest/PullRequestCandidatePicker.test.tsx
rename to apps/web/src/components/pullRequest/PullRequestCandidatePicker.browser.test.tsx
index 2fb8a2a4449..a397e5d04f4 100644
--- a/apps/web/src/components/pullRequest/PullRequestCandidatePicker.test.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestCandidatePicker.browser.test.tsx
@@ -1,8 +1,7 @@
-// @vitest-environment jsdom
-
import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
+import { userEvent } from "vite-plus/test/browser";
import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker";
@@ -11,14 +10,6 @@ let container: HTMLDivElement;
beforeEach(() => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
- vi.stubGlobal(
- "ResizeObserver",
- class {
- observe() {}
- unobserve() {}
- disconnect() {}
- },
- );
container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
@@ -28,7 +19,6 @@ afterEach(async () => {
await act(async () => root.unmount());
container.remove();
vi.unstubAllGlobals();
- vi.useRealTimers();
});
describe("disabled candidate picker", () => {
@@ -69,27 +59,20 @@ describe("disabled candidate picker", () => {
'button[aria-label="Request reviewers"]',
)!;
if (input === "keyboard") {
- await act(async () => {
- document.dispatchEvent(new KeyboardEvent("keydown", { key: "Tab", bubbles: true }));
- trigger.focus();
- });
+ await act(() => userEvent.tab());
expect(document.activeElement).toBe(trigger);
} else {
- vi.useFakeTimers();
- await act(async () => {
- trigger.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
- trigger.dispatchEvent(new MouseEvent("mouseenter"));
- trigger.dispatchEvent(new MouseEvent("mousemove", { bubbles: true }));
- await vi.advanceTimersByTimeAsync(1000);
- });
+ await act(() => userEvent.hover(trigger));
}
- expect(
- document.querySelector('[data-slot="tooltip-popup"][data-open]')?.textContent,
- ).toContain("Only collaborators can request reviewers.");
+ await expect
+ .poll(() => document.querySelector('[data-slot="tooltip-popup"][data-open]')?.textContent)
+ .toContain("Only collaborators can request reviewers.");
await act(async () => {
- trigger.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
- trigger.dispatchEvent(new KeyboardEvent("keydown", { key: " ", bubbles: true }));
- trigger.click();
+ trigger.focus();
+ await userEvent.keyboard("{Enter}");
+ await userEvent.keyboard(" ");
+ // Playwright skips aria-disabled targets; the click must still land to prove it is inert.
+ await userEvent.click(trigger, { force: true });
});
expect(onOpenChange).not.toHaveBeenCalled();
expect(onSelect).not.toHaveBeenCalled();
diff --git a/apps/web/src/components/settings/FontFamilyPicker.test.tsx b/apps/web/src/components/settings/FontFamilyPicker.browser.test.tsx
similarity index 67%
rename from apps/web/src/components/settings/FontFamilyPicker.test.tsx
rename to apps/web/src/components/settings/FontFamilyPicker.browser.test.tsx
index edd30adb411..997fcae85b4 100644
--- a/apps/web/src/components/settings/FontFamilyPicker.test.tsx
+++ b/apps/web/src/components/settings/FontFamilyPicker.browser.test.tsx
@@ -1,8 +1,7 @@
-// @vitest-environment jsdom
-
import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
+import { userEvent } from "vite-plus/test/browser";
const families = vi.hoisted(() => {
const families = [
@@ -32,23 +31,6 @@ let container: HTMLDivElement;
beforeEach(async () => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
- vi.stubGlobal(
- "ResizeObserver",
- class {
- observe() {}
- disconnect() {}
- unobserve() {}
- },
- );
- // jsdom has no layout; give LegendList a 30px viewport and 30px rows so it
- // renders a window of rows the way it does in a browser.
- vi.spyOn(Element.prototype, "getBoundingClientRect").mockReturnValue(
- DOMRect.fromRect({ width: 288, height: 30 }),
- );
- Object.defineProperty(Element.prototype, "getAnimations", {
- configurable: true,
- value: () => [],
- });
container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
@@ -68,14 +50,6 @@ function highlightedOptions(): string[] {
);
}
-async function typeQuery(input: HTMLInputElement, value: string) {
- const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
- await act(async () => {
- setValue.call(input, value);
- input.dispatchEvent(new Event("input", { bubbles: true }));
- });
-}
-
describe("FontFamilyPicker", () => {
it("highlights, moves through, and selects matches after filtering", async () => {
const onSelect = vi.fn();
@@ -93,14 +67,11 @@ describe("FontFamilyPicker", () => {
const input = document.querySelector<HTMLInputElement>('input[placeholder="Search fonts…"]');
expect(input).not.toBeNull();
- const press = (key: string) =>
- act(async () => {
- input!.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }));
- });
+ const press = (key: string) => act(() => userEvent.keyboard(`{${key}}`));
// Both matches were already rendered further down the unfiltered list.
- await typeQuery(input!, "co");
- expect(highlightedOptions()).toEqual(["Courier New"]);
+ await act(() => userEvent.type(input!, "co"));
+ await expect.poll(highlightedOptions).toEqual(["Courier New"]);
await press("ArrowDown");
expect(highlightedOptions()).toEqual(["Fira Code"]);
diff --git a/apps/web/src/components/usage/UsageLimitsPooled.test.tsx b/apps/web/src/components/usage/UsageLimitsPooled.browser.test.tsx
similarity index 90%
rename from apps/web/src/components/usage/UsageLimitsPooled.test.tsx
rename to apps/web/src/components/usage/UsageLimitsPooled.browser.test.tsx
index f76ad9e9b3f..27ce493f775 100644
--- a/apps/web/src/components/usage/UsageLimitsPooled.test.tsx
+++ b/apps/web/src/components/usage/UsageLimitsPooled.browser.test.tsx
@@ -1,5 +1,3 @@
-// @vitest-environment jsdom
-
import {
EnvironmentId,
ProviderDriverKind,
@@ -10,6 +8,10 @@ import type { LimitPresentations } from "@t3tools/shared/usageLimits";
import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
+import { userEvent } from "vite-plus/test/browser";
+
+// Real pointer clicks need the dialog's stacking styles to land above Base UI's modal overlay.
+import "~/index.css";
const consume = vi.hoisted(() => vi.fn());
vi.mock("../../state/session", () => ({
@@ -71,14 +73,6 @@ let container: HTMLDivElement;
beforeEach(async () => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
- vi.stubGlobal(
- "ResizeObserver",
- class {
- observe() {}
- disconnect() {}
- unobserve() {}
- },
- );
consume.mockReset();
container = document.createElement("div");
document.body.append(container);
@@ -97,7 +91,7 @@ async function click(label: string) {
(node) => node.textContent === label || node.getAttribute("aria-label")?.startsWith(label),
);
expect(button).toBeDefined();
- await act(() => button!.click());
+ await act(() => userEvent.click(button!));
}
it.each([
@@ -123,8 +117,9 @@ it.each([
await click("Use reset");
await click("Use credit");
expect(consume).toHaveBeenCalledOnce();
- expect(document.querySelector('[role="alertdialog"]')).toBeNull();
- expect(document.querySelector('[data-slot="popover-popup"]')).toBeNull();
+ // Both popups unmount once their exit animations finish.
+ await expect.poll(() => document.querySelector('[role="alertdialog"]')).toBeNull();
+ await expect.poll(() => document.querySelector('[data-slot="popover-popup"]')).toBeNull();
const status = document.querySelector('[role="status"]');
expect(status?.textContent).toBe("Alpha Using reset credit…");
await click("Alpha:");