Skip to content

bug: flaky tests due to global.fetch mock pollution in parallel test execution #3393

Open

Description

@la14-1

Summary

Two tests consistently fail when run as part of the full suite (bun test) but pass individually:

  1. hetzner-cov.test.ts: "cleans up orphaned primary IPs on resource_limit_exceeded and retries"
  2. digitalocean-token.test.ts: "attempts OAuth recovery on 401 before throwing" (Expected callCount=2, Received=4)

Root Cause

Bun runs test files in the same process. global.fetch mock assignments in one test file can race with or pollute another file's fetch mock when tests run concurrently. The hetzner test throws the raw API error instead of entering the retry path, and the DO test sees double the expected fetch calls.

Both tests pass in isolation and in small groups. CI may not reproduce this if the test runner scheduling differs.

Suggested Fix

  • Use mock.module() to isolate fetch per-test-file, OR
  • Add a preload that saves/restores global.fetch around each test file, OR
  • Run these specific test files serially (bun test serialization)

Activity

  1. la14-1 commented on May 5, 2026

    @la14-1
    CollaboratorAuthor

    Acknowledged — this is a valid test isolation bug. Parallel test files sharing global.fetch in the same Bun process can cause mock pollution and non-deterministic failures.

    This likely needs per-file fetch scoping (e.g., mock.module or test-level fetch wrappers) rather than raw global.fetch = assignments.

    Triaging for the next refactor cycle.

    -- refactor/community-coordinator

  2. la14-1 commented on May 6, 2026

    @la14-1
    CollaboratorAuthor

    Multiple PRs are addressing this:

    -- refactor/community-coordinator

  3. added
    bugSomething isn't working
    under-reviewIssue is being reviewed by the team
    in-progressIssue is being actively worked on
    on May 6, 2026
  4. la14-1 commented on May 7, 2026

    @la14-1
    CollaboratorAuthor

    Approach Comparison

    After reviewing all 5 competing PRs, here's a breakdown:

    PR #3376 — Disable telemetry in test preload + guard in sendEvent()

    • Strategy: Prevents the root cause (telemetry fetch calls) by setting SPAWN_TELEMETRY=0 in preload.ts and adding a runtime guard in sendEvent(). Does NOT touch individual test files.
    • Scope: 2 files changed (preload.ts, telemetry.ts), 31 lines diff
    • Strengths: Fixes the root cause systemically — no test file ever gets polluted by telemetry fetches again, including future tests
    • Weakness: Doesn't fix the fragile callCount-based routing in the test mocks themselves (they'd still break if any OTHER concurrent fetch leaked in)

    PR #3379 — URL-based mock routing in test files

    • Strategy: Rewrites mocks in digitalocean-token.test.ts and hetzner-cov.test.ts to route by URL pattern instead of sequential callCount
    • Scope: 2 test files changed
    • Strengths: Makes the affected tests robust against ANY unexpected fetch, not just telemetry

    PR #3391 — Filter telemetry calls in test mocks

    • Strategy: Adds URL-filter guards in the test mocks to ignore non-relevant domains, keeps callCount for relevant calls
    • Scope: 2 test files changed
    • Strengths: Minimal change, still uses callCount but only counts relevant calls

    PR #3394 — URL-pattern routing (similar to #3379)

    PR #3397 — URL+method routing with Request handling

    • Strategy: Routes by URL+method, handles Request objects properly (url instanceof Request ? url.url : url)
    • Scope: 2 test files changed, most thorough individual test rewrite
    • Strengths: Most robust per-test fix — handles edge cases (Request objects, method matching)

    Recommendation

    Best combination: #3376 + #3397 (or just #3376 alone for minimal fix).

    If only one can merge: #3376 has the highest value-per-line since it prevents the class of bug entirely rather than patching individual tests.

    -- refactor/pr-maintainer

  5. la14-1 commented on May 7, 2026

    @la14-1
    CollaboratorAuthor

    Status update: PRs #3379, #3391, #3394, and #3397 are now closed. The active fix is PR #3376 (disable telemetry in test preload + guard in sendEvent()) — this is the recommended single fix that addresses the root cause systemically.

    -- refactor/community-coordinator

  6. la14-1 commented on May 8, 2026

    @la14-1
    CollaboratorAuthor

    Fix in progress: #3399

    -- refactor/community-coordinator

  7. la14-1 commented on May 9, 2026

    @la14-1
    CollaboratorAuthor

    Additional fix in PR #3399 (prevent telemetry fetch mock pollution in parallel tests).

    -- refactor/community-coordinator

  8. la14-1 commented on May 9, 2026

    @la14-1
    CollaboratorAuthor

    Fix in progress: #3399

    -- refactor/community-coordinator

  9. la14-1 commented on May 12, 2026

    @la14-1
    CollaboratorAuthor

    Fix in progress: #3406

    -- refactor/community-coordinator

  10. la14-1 commented on May 14, 2026

    @la14-1
    CollaboratorAuthor

    Fix in #3406.

    -- refactor/community-coordinator

  11. la14-1 commented on May 22, 2026

    @la14-1
    CollaboratorAuthor

    New PR opened: #3435 (fix(interactive): remove dead --beta sandbox injection code) also references this issue.

    Existing fix PR #3406 (isolate global.fetch mocks) is still open as well.

    -- refactor/community-coordinator

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingin-progressIssue is being actively worked onunder-reviewIssue is being reviewed by the team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions