@devries-cameron20 / t3code Mirror · read-only / commits / 7895a2bc9d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 20:45 UTC

test(web): ban frame and transition waits in browser tests (#22561)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 20, 2026 at 18:05 UTC 7895a2bc9d419d9868f038226a6a6dcf442bee66
4 files changed +252
oxlint-plugin-t3code/index.ts
+2
@@ -2,6 +2,7 @@ import { definePlugin } from "@oxlint/plugins";
2
3 import namespaceNodeImports from "./rules/namespace-node-imports.ts";
4 import noDomTestEnvironment from "./rules/no-dom-test-environment.ts";
5 +import noFrameWaitsInBrowserTests from "./rules/no-frame-waits-in-browser-tests.ts";
6 import noGlobalProcessRuntime from "./rules/no-global-process-runtime.ts";
7 import noHermesUnsupportedApis from "./rules/no-hermes-unsupported-apis.ts";
8 import noHooklessUseFunction from "./rules/no-hookless-use-function.ts";
@@ -32,6 +33,7 @@ export default definePlugin({
33 rules: {
34 "namespace-node-imports": namespaceNodeImports,
35 "no-dom-test-environment": noDomTestEnvironment,
36 + "no-frame-waits-in-browser-tests": noFrameWaitsInBrowserTests,
37 "no-global-process-runtime": noGlobalProcessRuntime,
38 "no-hermes-unsupported-apis": noHermesUnsupportedApis,
39 "no-hookless-use-function": noHooklessUseFunction,
oxlint-plugin-t3code/rules/no-frame-waits-in-browser-tests.test.ts new
+96
@@ -0,0 +1,96 @@
1 +import { describe } from "@effect/vitest";
2 +
3 +import { createOxlintRuleHarness } from "../test/utils.ts";
4 +
5 +const browserTest = createOxlintRuleHarness("t3code/no-frame-waits-in-browser-tests", {
6 + filename: "Widget.browser.test.tsx",
7 +});
8 +const unitTest = createOxlintRuleHarness("t3code/no-frame-waits-in-browser-tests", {
9 + filename: "widget.test.tsx",
10 +});
11 +
12 +describe("t3code/no-frame-waits-in-browser-tests", () => {
13 + browserTest.valid(
14 + "allows retrying assertions and the full fake timer set",
15 + [
16 + 'it("debounces", async () => {',
17 + " await using screen = await render(null);",
18 + " using _timers = fakeTimers();",
19 + " vi.advanceTimersByTime(200);",
20 + ' await expect.element(screen.getByRole("button")).toBeVisible();',
21 + " await vi.waitFor(() => expect(onSave).toHaveBeenCalled());",
22 + "});",
23 + ].join("\n"),
24 + );
25 + browserTest.valid(
26 + "allows requestAnimationFrame inside mocks",
27 + [
28 + 'vi.spyOn(window, "requestAnimationFrame").mockImplementation((callback) => {',
29 + " callback(0);",
30 + " return 0;",
31 + "});",
32 + "const frame = vi.fn((callback) => requestAnimationFrame(callback));",
33 + 'vi.stubGlobal("requestAnimationFrame", (callback) => window.requestAnimationFrame(callback));',
34 + 'stubGlobal("requestAnimationFrame", (callback) => requestAnimationFrame(callback));',
35 + ].join("\n"),
36 + );
37 + browserTest.valid(
38 + "allows timers outside a promise executor and other listeners",
39 + [
40 + "const id = setTimeout(() => undefined, 10);",
41 + 'panel.addEventListener("click", () => undefined);',
42 + "const deferred = new Promise((resolve) => (finish = resolve));",
43 + ].join("\n"),
44 + );
45 +
46 + browserTest.invalid(
47 + "rejects a nextFrame helper",
48 + "const nextFrame = () => new Promise((resolve) => requestAnimationFrame(() => resolve()));",
49 + );
50 + browserTest.invalid(
51 + "rejects requestAnimationFrame as the executor",
52 + "await new Promise(requestAnimationFrame);",
53 + );
54 + browserTest.invalid(
55 + "rejects window.requestAnimationFrame",
56 + "window.requestAnimationFrame(() => undefined);",
57 + );
58 + browserTest.valid(
59 + "allows a pass-through frame executor inside a mock implementation",
60 + "const paint = vi.fn(() => new Promise(requestAnimationFrame));",
61 + );
62 + browserTest.invalid(
63 + "rejects setTimeout passed straight through as the promise executor",
64 + "await new Promise(setTimeout);",
65 + );
66 + browserTest.invalid(
67 + "rejects setTimeout inside a promise executor",
68 + "await new Promise((resolve) => setTimeout(resolve, 50));",
69 + );
70 + browserTest.invalid(
71 + "rejects setInterval inside a promise executor",
72 + "await new Promise(function (resolve) { window.setInterval(resolve, 50); });",
73 + );
74 + browserTest.invalid(
75 + "rejects transitionend listeners",
76 + 'panel.addEventListener("transitionend", () => resolve(), { once: true });',
77 + );
78 + browserTest.invalid(
79 + "rejects animationend listeners",
80 + 'panel.addEventListener("animationend", () => resolve());',
81 + );
82 + browserTest.invalid("rejects vi.useFakeTimers", "vi.useFakeTimers();");
83 + browserTest.invalid(
84 + "rejects a partial fakeTimers set",
85 + 'const timers = fakeTimers({ toFake: ["setTimeout", "clearTimeout"] });',
86 + );
87 + browserTest.invalid(
88 + "rejects a partial vi.useFakeTimers set",
89 + 'vi.useFakeTimers({ toFake: ["setTimeout"] });',
90 + );
91 +
92 + unitTest.valid(
93 + "ignores files that are not browser tests",
94 + ["vi.useFakeTimers();", "await new Promise(requestAnimationFrame);"].join("\n"),
95 + );
96 +});
oxlint-plugin-t3code/rules/no-frame-waits-in-browser-tests.ts new
+150
@@ -0,0 +1,150 @@
1 +import { defineRule, type ESTree } from "@oxlint/plugins";
2 +import * as Option from "effect/Option";
3 +
4 +import { getPropertyName, isIdentifier, unwrapExpression } from "../utils.ts";
5 +
6 +const BROWSER_TEST_FILE_PATTERN = /\.browser\.test\.[cm]?[jt]sx?$/u;
7 +const GLOBAL_OBJECTS = new Set(["globalThis", "self", "window"]);
8 +const TIMER_FUNCTIONS = new Set(["setInterval", "setTimeout"]);
9 +const TRANSITION_EVENTS = new Set(["animationend", "transitionend"]);
10 +const MOCK_FACTORIES = new Set(["fn", "spyOn", "stubGlobal"]);
11 +const MOCK_IMPLEMENTATIONS = new Set(["mockImplementation", "mockImplementationOnce"]);
12 +const USE_FAKE_TIMERS = new Set(["useFakeTimers"]);
13 +const FUNCTIONS = new Set(["ArrowFunctionExpression", "FunctionExpression"]);
14 +
15 +const MESSAGE =
16 + "Frame and transition waits race the renderer. Assert with `expect.element`/`vi.waitFor` (auto-retry), or install `fakeTimers()` (full set) and advance the timer the component sets.";
17 +
18 +/** Name of a bare global (`requestAnimationFrame`) or one read off `window`/`globalThis`/`self`. */
19 +const globalName = (node: unknown): Option.Option<string> => {
20 + const expression = unwrapExpression(node);
21 + if (Option.isNone(expression)) return Option.none();
22 + if (expression.value.type === "Identifier") return Option.some(expression.value.name);
23 + if (expression.value.type !== "MemberExpression") return Option.none();
24 + const object = unwrapExpression(expression.value.object);
25 + if (Option.isNone(object) || object.value.type !== "Identifier") return Option.none();
26 + if (!GLOBAL_OBJECTS.has(object.value.name)) return Option.none();
27 + return getPropertyName(expression.value.property);
28 +};
29 +
30 +const isViCall = (callee: unknown, names: ReadonlySet<string>): boolean => {
31 + const expression = unwrapExpression(callee);
32 + if (Option.isNone(expression) || expression.value.type !== "MemberExpression") return false;
33 + const property = getPropertyName(expression.value.property);
34 + return (
35 + isIdentifier(unwrapExpression(expression.value.object), "vi") &&
36 + Option.isSome(property) &&
37 + names.has(property.value)
38 + );
39 +};
40 +
41 +const isMockingCall = (node: ESTree.CallExpression): boolean => {
42 + if (isViCall(node.callee, MOCK_FACTORIES)) return true;
43 + const callee = unwrapExpression(node.callee);
44 + if (isIdentifier(callee, "stubGlobal")) return true;
45 + if (Option.isNone(callee) || callee.value.type !== "MemberExpression") return false;
46 + const property = getPropertyName(callee.value.property);
47 + return Option.isSome(property) && MOCK_IMPLEMENTATIONS.has(property.value);
48 +};
49 +
50 +const EXECUTOR_WAITS = new Set(["requestAnimationFrame", "setTimeout", "setInterval"]);
51 +
52 +const isPromiseConstructor = (node: ESTree.Node | null | undefined): node is ESTree.NewExpression =>
53 + node?.type === "NewExpression" && isIdentifier(unwrapExpression(node.callee), "Promise");
54 +
55 +// A frame call inside a mock is the fake's implementation, not the test waiting on a frame.
56 +const isInsideMock = (node: ESTree.Node): boolean => {
57 + let child: ESTree.Node = node;
58 + let current = node.parent;
59 + while (current) {
60 + if (current.type === "CallExpression" && current.callee !== child && isMockingCall(current)) {
61 + return true;
62 + }
63 + child = current;
64 + current = current.parent;
65 + }
66 + return false;
67 +};
68 +
69 +const isInsidePromiseExecutor = (node: ESTree.Node): boolean => {
70 + let current = node.parent;
71 + while (current) {
72 + if (
73 + FUNCTIONS.has(current.type) &&
74 + isPromiseConstructor(current.parent) &&
75 + current.parent.arguments[0] === current
76 + ) {
77 + return true;
78 + }
79 + current = current.parent;
80 + }
81 + return false;
82 +};
83 +
84 +const hasToFakeOption = (argument: unknown): boolean => {
85 + const expression = unwrapExpression(argument);
86 + if (Option.isNone(expression) || expression.value.type !== "ObjectExpression") return false;
87 + return expression.value.properties.some((property) => {
88 + if (property.type !== "Property") return false;
89 + const key = getPropertyName(property.key);
90 + return Option.isSome(key) && key.value === "toFake";
91 + });
92 +};
93 +
94 +const isTransitionEventListener = (node: ESTree.CallExpression): boolean => {
95 + const callee = unwrapExpression(node.callee);
96 + if (Option.isNone(callee) || callee.value.type !== "MemberExpression") return false;
97 + const method = getPropertyName(callee.value.property);
98 + if (Option.isNone(method) || method.value !== "addEventListener") return false;
99 + const eventName = unwrapExpression(node.arguments[0]);
100 + return (
101 + Option.isSome(eventName) &&
102 + eventName.value.type === "Literal" &&
103 + typeof eventName.value.value === "string" &&
104 + TRANSITION_EVENTS.has(eventName.value.value)
105 + );
106 +};
107 +
108 +const isFrameOrTimerWait = (node: ESTree.CallExpression): boolean => {
109 + const name = globalName(node.callee);
110 + if (Option.isNone(name)) return false;
111 + if (name.value === "requestAnimationFrame") return !isInsideMock(node);
112 + if (TIMER_FUNCTIONS.has(name.value)) return isInsidePromiseExecutor(node);
113 + if (name.value === "fakeTimers") return hasToFakeOption(node.arguments[0]);
114 + return false;
115 +};
116 +
117 +export default defineRule({
118 + meta: {
119 + type: "problem",
120 + docs: {
121 + description:
122 + "Disallow waiting on animation frames, transitions, or real timers in browser tests, and partial fake-timer sets.",
123 + },
124 + },
125 + create(context) {
126 + if (!BROWSER_TEST_FILE_PATTERN.test(context.filename)) return {};
127 +
128 + return {
129 + NewExpression(node) {
130 + if (!isPromiseConstructor(node) || isInsideMock(node)) return;
131 + const executor = node.arguments[0];
132 + // `new Promise(setTimeout)` resolves on the next macrotask; `new Promise(requestAnimationFrame)`
133 + // on the next frame. Both are the same wait with the function passed straight through.
134 + const name = globalName(executor);
135 + if (executor && Option.isSome(name) && EXECUTOR_WAITS.has(name.value)) {
136 + context.report({ node: executor, message: MESSAGE });
137 + }
138 + },
139 + CallExpression(node) {
140 + if (
141 + isFrameOrTimerWait(node) ||
142 + isViCall(node.callee, USE_FAKE_TIMERS) ||
143 + isTransitionEventListener(node)
144 + ) {
145 + context.report({ node: node.callee, message: MESSAGE });
146 + }
147 + },
148 + };
149 + },
150 +});
vite.config.ts
+4
@@ -211,6 +211,10 @@ export default defineConfig({
211 files: ["apps/web/**/*.test.{ts,tsx}"],
212 rules: { "t3code/require-disposable-render": "error" },
213 },
214 + {
215 + files: ["apps/web/src/**/*.browser.test.{ts,tsx}"],
216 + rules: { "t3code/no-frame-waits-in-browser-tests": "error" },
217 + },
218 {
219 files: ["apps/**/src/**", "packages/**/src/**"],
220 excludeFiles: DOM_TEST_ENVIRONMENT_ALLOWLIST,