Skip to content

test(e2e): named per-role fixtures (alice, bob, multiTab) - #12

Merged
iskhakovt merged 2 commits into
mainfrom
feat/e2e-named-fixtures
Apr 11, 2026
Merged

iskhakovt merged 2 commits into
mainfrom
feat/e2e-named-fixtures

Conversation

@iskhakovt

@iskhakovt iskhakovt commented Apr 11, 2026 •

Copy link
Copy Markdown
Owner

Summary

Addresses the final CR comment on #9: manual browser.newContext() instances leak on test failure because Playwright does not auto-dispose contexts created outside its built-in fixtures. Rather than add try/finally blocks to every test (CR's suggestion) or use a factory escape hatch, land named per-role fixtures in fixtures.ts.

New fixtures

export const test = base.extend<{
  alice: Page;
  bob: Page;
  multiTab: { ctx: BrowserContext; admin: Page };
}>({
  alice: async ({ browser }, use) => { /* isolated ctx + page */ },
  bob: async ({ browser }, use) => { /* isolated ctx + page */ },
  multiTab: async ({ browser }, use) => { /* shared ctx + admin page */ },
});

Test signatures now self-document

Topology Before After
Two-user async ({ browser }) => { const aliceCtx = await browser.newContext(); /* ... */ } async ({ alice, bob }) => { ... }
Multi-tab async ({ browser }) => { const ctx = await browser.newContext(); const admin = await ctx.newPage(); /* ... */ } async ({ multiTab: { ctx, admin } }) => { ... }
Single-user Alice (drive-by fix) manual context + page async ({ page }) => { ... } (built-in, no manual context needed)

Why this design

  • Lazy fixture evaluation: a test that only destructures { alice } never creates a Bob context. No wasted work for single-user scenarios.
  • Type-safe & composable: adding roles means adding fixtures; no ad-hoc bundling.
  • No escape hatch needed: every topology has a named fixture.
  • Canonical per Playwright's fixture docs — no factory pattern, no manual lifecycle.

Files touched

  • e2e/fixtures.ts — new alice / bob / multiTab fixtures
  • e2e/edit-post-completion.spec.ts — 2 tests migrated; the single-user waiting test moved to built-in { page }
  • e2e/multi-tab.spec.ts — 3 tests migrated to multiTab
  • e2e/realtime-status.spec.ts — 1 test migrated
  • e2e/reconnect-resume.spec.ts — 1 test migrated
  • e2e/results.spec.ts — 3 tests migrated
  • e2e/self-pick-flow.spec.ts — 1 test migrated
  • e2e/two-player.spec.ts — 1 test migrated (plain + encrypted)

Zero manual browser.newContext() calls remain in the suite.

Test plan

  • pnpm exec biome check .
  • pnpm --filter @spreadsheet/web typecheck
  • pnpm exec playwright test --workers=2 — 35/35 passing, ~3 min
  • CI

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Updated end-to-end tests to use custom Playwright test fixtures for consistent page and browser context provisioning. Refactored multiple e2e tests to rely on fixture-managed pages/contexts, removing manual context/page creation and explicit cleanup while preserving all existing interaction flows and assertions.

@coderabbitai

coderabbitai Bot commented Apr 11, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e9df1def-5f8a-4033-a380-0bb17ff1fcdb

📥 Commits

Reviewing files that changed from the base of the PR and between 2cdd150 and ceb8278.

📒 Files selected for processing (1)
  • e2e/edit-post-completion.spec.ts

📝 Walkthrough

Walkthrough

These changes replace manual Playwright context/page creation with new fixtures (alice, bob, multiTab) and update tests to use those fixtures, removing explicit context teardown while preserving existing test flows and assertions.

Changes

Cohort / File(s) Summary
Fixture Definitions
e2e/fixtures.ts
Added typed Playwright fixtures alice: Page, bob: Page, and multiTab: { ctx: BrowserContext; admin: Page } that create new BrowserContext/Page instances and perform automatic teardown (context close with error swallow).
Tests Using alice/bob Fixtures
e2e/edit-post-completion.spec.ts, e2e/realtime-status.spec.ts, e2e/reconnect-resume.spec.ts, e2e/results.spec.ts, e2e/self-pick-flow.spec.ts, e2e/two-player.spec.ts
Refactored tests to accept fixture-injected alice/bob/page parameters instead of calling browser.newContext()/newPage(). Removed explicit ctx.close()/page.close() cleanup; preserved navigation, interaction, and assertions.
Tests Using multiTab Fixture
e2e/multi-tab.spec.ts
Updated multi-tab isolation tests to use the multiTab fixture ({ ctx, admin }) for shared context and admin page; removed manual context creation/closure and kept test logic unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰✨ I hopped in with fixtures bright,
New contexts spun without a fight,
No more closing doors by hand,
Pages dance on steady land,
Tests now hop together—light!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and accurately summarizes the main change: introducing named per-role fixtures (alice, bob, multiTab) for e2e tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/e2e-named-fixtures

Comment @coderabbitai help to get the list of available commands and usage tips.

Base automatically changed from feat/tanstack-query-migration to main April 11, 2026 18:06
…xtures

Addresses the final CR comment on #9: manual `browser.newContext()`
instances are not auto-disposed by Playwright. If a test fails
mid-flow, contexts leak until the worker exits, which can starve
memory and produce incomplete traces.

Instead of adding try/finally blocks to every test (CR's suggestion)
or a factory escape hatch, land named per-role fixtures in
fixtures.ts:

  - `alice` / `bob` — each provides a Page inside its own fresh
    BrowserContext. Two-user tests destructure whichever they need;
    Playwright's fixture-level lazy evaluation ensures unused
    fixtures never create contexts.
  - `multiTab` — single shared BrowserContext + pre-created admin
    Page. Used only by `multi-tab.spec.ts` which intentionally
    tests cross-tab localStorage isolation within one browser.

Every per-test manual `browser.newContext()` + `ctx.close()` pair
is gone. Test signatures now read as what they need:
  async ({ alice, bob }) => { ... }
  async ({ multiTab: { ctx, admin } }) => { ... }

Also fixed: the Alice-only `/waiting` "Edit my answers" test in
`edit-post-completion.spec.ts` was previously creating a manual
context for no reason — it's now on the built-in `{ page }` fixture
since it's a genuinely single-user test.

Why this pattern instead of a factory:
  - Self-documenting signatures (the test parameters tell you the
    topology before you read the body)
  - Lazy fixture evaluation: a `{ alice }` test doesn't create a
    Bob context
  - Type-safe, composable: adding roles means adding fixtures
  - Canonical per Playwright's fixture docs — no escape hatch needed

Files touched: fixtures.ts + the 7 specs that previously used
`browser.newContext()` manually.

Refs: coderabbitai review on #9 (edit-post-completion.spec.ts:10)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@iskhakovt
iskhakovt force-pushed the feat/e2e-named-fixtures branch from 6ba00b7 to 2cdd150 Compare April 11, 2026 18:10

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
e2e/fixtures.ts (2)

54-71: Reduce duplicated role fixture setup/teardown.

Lines 54-71 repeat the same context/page lifecycle for alice and bob. Extracting a small helper will keep fixture changes safer and more maintainable.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@e2e/fixtures.ts` around lines 54 - 71, The alice and bob fixtures repeat
identical setup/teardown logic; extract a shared helper (e.g.,
createRoleFixture) that accepts the fixture callback and returns an async
fixture to create a new context and page, call use(page), and close the context
with .catch(() => {}); replace alice and bob implementations to delegate to this
helper and keep multiTab unchanged (it already differs) so future changes to
lifecycle live in one place.

36-40: Extract fixture object shapes into interfaces.

Lines 36-40 currently use inline object shapes for exported fixture typing. Please move these to named interfaces/types (PascalCase) to match repo TypeScript conventions.

♻️ Proposed refactor
 import { type BrowserContext, test as base, expect, type Page } from "@playwright/test";
@@
-export const test = base.extend<{
-  alice: Page;
-  bob: Page;
-  multiTab: { ctx: BrowserContext; admin: Page };
-}>({
+interface MultiTabFixture {
+  ctx: BrowserContext;
+  admin: Page;
+}
+
+interface E2eFixtures {
+  alice: Page;
+  bob: Page;
+  multiTab: MultiTabFixture;
+}
+
+export const test = base.extend<E2eFixtures>({

As per coding guidelines: "Use interfaces for object shapes in TypeScript".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@e2e/fixtures.ts` around lines 36 - 40, The inline fixture shape used in the
exported const test (base.extend<{ alice: Page; bob: Page; multiTab: { ctx:
BrowserContext; admin: Page }; }>) should be extracted into PascalCase
interfaces; define e.g. interface TestFixtures { alice: Page; bob: Page;
multiTab: MultiTabFixture } and interface MultiTabFixture { ctx: BrowserContext;
admin: Page }, export them, then replace the inline generic on base.extend with
base.extend<TestFixtures> so the fixture types follow the repo TypeScript
convention and are reusable.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@e2e/fixtures.ts`:
- Around line 54-71: The alice and bob fixtures repeat identical setup/teardown
logic; extract a shared helper (e.g., createRoleFixture) that accepts the
fixture callback and returns an async fixture to create a new context and page,
call use(page), and close the context with .catch(() => {}); replace alice and
bob implementations to delegate to this helper and keep multiTab unchanged (it
already differs) so future changes to lifecycle live in one place.
- Around line 36-40: The inline fixture shape used in the exported const test
(base.extend<{ alice: Page; bob: Page; multiTab: { ctx: BrowserContext; admin:
Page }; }>) should be extracted into PascalCase interfaces; define e.g.
interface TestFixtures { alice: Page; bob: Page; multiTab: MultiTabFixture } and
interface MultiTabFixture { ctx: BrowserContext; admin: Page }, export them,
then replace the inline generic on base.extend with base.extend<TestFixtures> so
the fixture types follow the repo TypeScript convention and are reusable.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 41e0d46d-b909-4182-ab20-63b9262c3799

📥 Commits

Reviewing files that changed from the base of the PR and between ef8bcee and 2cdd150.

📒 Files selected for processing (8)
  • e2e/edit-post-completion.spec.ts
  • e2e/fixtures.ts
  • e2e/multi-tab.spec.ts
  • e2e/realtime-status.spec.ts
  • e2e/reconnect-resume.spec.ts
  • e2e/results.spec.ts
  • e2e/self-pick-flow.spec.ts
  • e2e/two-player.spec.ts

The test signature (`async ({ page }) => ...`) already communicates
that this is a single-user test using the built-in page fixture.
The comment restates what the code says.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@iskhakovt

Copy link
Copy Markdown
Owner Author

Response to review

Both nitpicks are pure style preferences and don't hold up on inspection. Rejecting both.

Nitpick 1: extract createRoleFixture helper

The duplication is 4 lines of obvious code (newContext → newPage → use → close) repeated twice. Extracting a factory replaces it with another function whose body is the same 4 lines, used exactly twice. Net: one level of indirection added, roughly the same LOC, zero readability gain.

This is below the abstraction threshold. The rule of thumb in this codebase (and generally) is "three similar lines of code is better than a premature abstraction" — two-copy duplication of 4 trivial lines doesn't cross it. You want to see the pattern three or more times before factoring, because that's when the cost of reading two versions exceeds the cost of reading the abstraction plus its call sites.

If a third role appears (Carol, Dave, etc.), the duplication multiplies and extracting pays off. Right now we have Alice + Bob and no plans to add more. YAGNI — extract when the need materializes, not preemptively.

Nitpick 2: extract TestFixtures / MultiTabFixture interfaces

Three problems with this suggestion:

  1. "Reusable" is false. The extracted types would be used in exactly one place — the base.extend<>() call. No other file in the repo imports fixture types. If nothing reuses a type, extracting it is just moving its definition further from its only use site, which is anti-readability.

  2. "Repo TypeScript convention" is false. Grep shows the codebase mixes type aliases and interface declarations with roughly equal frequency, and there's no lint rule enforcing one over the other. CR cited a convention that doesn't exist in this repo.

  3. The inline generic is canonical Playwright. Every example in Playwright's fixture docs uses the inline base.extend<{ ... }> form. Extracting it moves away from idiomatic usage, not toward it.

The strongest steelman is that the nested { ctx: BrowserContext; admin: Page } inside the generic is marginally noisy. Naming it MultiTabFixture would make multiTab: MultiTabFixture read slightly better in one spot. But the cost is two exported interfaces that live in the file forever for zero semantic win. Not worth it.

Drive-by cleanup (ceb8278)

Removed a redundant // Single-user test — uses built-in page fixture comment above the Alice-only waiting test. The test signature (async ({ page }) => ...) already communicates exactly that.

@iskhakovt
iskhakovt merged commit 38e4559 into main Apr 11, 2026
7 checks passed
@iskhakovt
iskhakovt deleted the feat/e2e-named-fixtures branch April 11, 2026 18:25
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.3.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant