TypeScript
test(web): require-disposable-render is now an error (#22410)
Julius Marminge6ab39ff6b8f6
diff --git a/AGENTS.md b/AGENTS.mdindex e595419d2fa..ad6c6a14363 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -100,7 +100,12 @@ An empty database is a bad test. Seed your worktree's `.t3` with a copy of real- **Do not run repo-wide checks.** No `vp check`, no `vp run -r test`, no `vp run -r typecheck` unless I ask. CI owns the full suite.- Backend behavior changes ship with focused tests for that behavior.- The server is event-sourced, and side effects run after the command commits. In tests, drain the effect worker (`OrchestrationEffectWorkerV2.drain`) or await the specific persisted event or `Deferred` that marks the milestone. Never wait on sleeps or polling. A test that needs a timeout to pass is wrong.-- Web component tests are `*.browser.test.tsx` and render through `apps/web/test/render.tsx`. Acquire everything a test changes with `using`/`await using` so disposal undoes it even when the test fails; no `beforeEach`/`afterEach`, no module-level screens. Shared mock state that `vi.mock` factories read gets reset by a `setup()` call at the top of the test. `t3code/require-disposable-render` flags the old patterns.+- Web browser tests are `*.browser.test.tsx` or `*.browser.test.ts` and render with `await using screen = await render(...)` from `apps/web/test/render.tsx`. `t3code/require-disposable-render` errors on the old patterns.+ - Acquire what the test drives after mounting (fake timers it advances, spies it sets up after mounting) after the render, so it disposes before the unmount. Anything the first render reads comes before it.+ - A helper that renders and then waits or clicks puts the screen in an `AsyncDisposableStack` and `move()`s it out only on success, so a failed wait still unmounts.+ - `rerender` needs a fresh element on every call.+ - No lifecycle hooks and no module-level mutable state. Shared mock state that `vi.mock` factories read is reset by a `setup()` call at the top of the test.+ - `router` is on by default. Pass `router: false` only with a one-line comment saying why.```tsxit("saves on Enter", async () => {diff --git a/oxlint-plugin-t3code/rules/require-disposable-render.test.ts b/oxlint-plugin-t3code/rules/require-disposable-render.test.tsindex 866c83bbc5f..bc69f362c01 100644--- a/oxlint-plugin-t3code/rules/require-disposable-render.test.ts+++ b/oxlint-plugin-t3code/rules/require-disposable-render.test.ts@@ -1,4 +1,4 @@-import { describe } from "@effect/vitest";+import { assert, describe } from "@effect/vitest";import { createOxlintRuleHarness } from "../test/utils.ts";@@ -18,6 +18,7 @@ describe("t3code/require-disposable-render", () => {'it("renders", async () => {'," await using screen = await render(null);"," await act(() => screen.rerender(null));",+ ' await userEvent.click(screen.getByRole("button"));',"});",].join("\n"),);@@ -39,8 +40,28 @@ describe("t3code/require-disposable-render", () => {"rejects stubbing the act environment",'vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);',);+ browserTest.invalid(+ "rejects userEvent wrapped in act from react",+ [+ 'import { act } from "react";',+ 'it("clicks", async () => {',+ " await act(async () => {",+ " await userEvent.click(document.body);",+ " });",+ "});",+ ].join("\n"),+ (output) => assert.match(output, /Do not wrap userEvent in React's act/),+ );+ browserTest.invalid(+ "rejects importing lifecycle hooks from the test runner",+ 'import { beforeEach, it } from "vite-plus/test";',+ );browserTest.invalid("rejects afterEach", "afterEach(async () => {});");browserTest.invalid("rejects beforeAll", "beforeAll(() => {});");unitTest.valid("allows lifecycle hooks outside browser tests", "beforeEach(() => {});");+ unitTest.valid(+ "allows importing lifecycle hooks outside browser tests",+ 'import { afterEach } from "vite-plus/test";',+ );unitTest.invalid("still rejects react-dom/client in unit tests", 'import "react-dom/client";');});diff --git a/oxlint-plugin-t3code/rules/require-disposable-render.ts b/oxlint-plugin-t3code/rules/require-disposable-render.tsindex da13e0e8924..fe8bd4c80de 100644--- a/oxlint-plugin-t3code/rules/require-disposable-render.ts+++ b/oxlint-plugin-t3code/rules/require-disposable-render.ts@@ -5,6 +5,7 @@ import { getPropertyName, unwrapExpression } from "../utils.ts";const BROWSER_TEST_FILE = /\.browser\.test\.[cm]?[jt]sx?$/u;const LIFECYCLE_HOOKS = new Set(["afterAll", "afterEach", "beforeAll", "beforeEach"]);+const TEST_RUNNERS = new Set(["@effect/vitest", "vite-plus/test", "vitest"]);const REACT_ACT_EXPORTS = new Set(["act", "unstable_act"]);const ACT_ENVIRONMENT = "IS_REACT_ACT_ENVIRONMENT";const HELPER = "Render with `await using screen = await render(...)` from apps/web/test/render.tsx";@@ -13,6 +14,8 @@ const MESSAGES = {reactDomClient: `${HELPER}; it owns the React root and unmounts it on dispose.`,reactAct: `${HELPER} and import \`act\` from there; it enables React's act environment only while act runs.`,actEnvironment: `${HELPER}; its act() sets ${ACT_ENVIRONMENT} for exactly as long as it runs.`,+ actAroundUserEvent:+ "Do not wrap userEvent in React's act; real input is not an act scope. Await the userEvent call directly.",lifecycleHook:"Browser tests acquire what they need with `using`/`await using` instead of lifecycle hooks, so nothing outlives the test that created it.",};@@ -41,9 +44,36 @@ export default defineRule({},create(context) {const isBrowserTest = BROWSER_TEST_FILE.test(context.filename);+ const reactActNames = new Set<string>();++ const isInsideReactAct = (node: ESTree.Node) => {+ for (let current = node.parent; current; current = current.parent) {+ if (+ current.type === "CallExpression" &&+ current.callee.type === "Identifier" &&+ reactActNames.has(current.callee.name)+ ) {+ return true;+ }+ }+ return false;+ };return {ImportDeclaration(node) {+ if (isBrowserTest && TEST_RUNNERS.has(node.source.value)) {+ for (const specifier of node.specifiers) {+ if (specifier.type !== "ImportSpecifier") continue;+ const imported =+ specifier.imported.type === "Identifier"+ ? specifier.imported.name+ : specifier.imported.value;+ if (LIFECYCLE_HOOKS.has(imported)) {+ context.report({ node: specifier, message: MESSAGES.lifecycleHook });+ }+ }+ return;+ }if (node.source.value === "react-dom/client") {context.report({ node: node.source, message: MESSAGES.reactDomClient });return;@@ -56,6 +86,7 @@ export default defineRule({? specifier.imported.name: specifier.imported.value;if (REACT_ACT_EXPORTS.has(imported)) {+ reactActNames.add(specifier.local.name);context.report({ node: specifier, message: MESSAGES.reactAct });}}@@ -71,6 +102,14 @@ export default defineRule({if (callee.value.type === "MemberExpression") {// vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true) leaves the flag on for the whole test.const method = getPropertyName(callee.value.property);+ if (+ callee.value.object.type === "Identifier" &&+ callee.value.object.name === "userEvent" &&+ isInsideReactAct(node)+ ) {+ context.report({ node, message: MESSAGES.actAroundUserEvent });+ return;+ }if (Option.getOrUndefined(method) === "stubGlobal" &&stringValue(node.arguments[0] as ESTree.Node | undefined) === ACT_ENVIRONMENTdiff --git a/vite.config.ts b/vite.config.tsindex 1d20d404656..3c63eae544e 100644--- a/vite.config.ts+++ b/vite.config.ts@@ -188,10 +188,8 @@ export default defineConfig({rules: { "t3code/prefer-effect-fn": "off" },},{- // Warns while the remaining web component tests move to apps/web/test/render.tsx.- // Raise to "error" once `vp lint` reports no t3code(require-disposable-render) warnings.files: ["apps/web/**/*.test.tsx"],- rules: { "t3code/require-disposable-render": "warn" },+ rules: { "t3code/require-disposable-render": "error" },},{files: ["apps/**/src/**", "packages/**/src/**"],