Skip to content

[CLAUDE ROUTINE]: Test-coverage enhancement — wrap vi.useFakeTimers/useRealTimers in try/finally (or afterEach) to prevent leaking fake timers across tests #173

Closed

Description

@NiveditJain

Summary

__tests__/components/copy-button.test.tsx enables fake timers inside a test body and restores real timers at the end of the same body. If the test throws between the two calls (an assertion failure, an unhandled promise rejection, …), real timers are never restored and every subsequent test in the same worker runs with fake timers still active — the classic "one red test turns the rest red" flakiness pattern.

Where

__tests__/components/copy-button.test.tsx (around the it(... 'shows copied state for 1.5s' ...) block)

vi.useFakeTimers({ shouldAdvanceTime: true });
render(<CopyButton text="test" />);
// ← if this block throws, real timers are never restored
vi.useRealTimers();

Why this helps users

Proposed enhancement

Prefer a global hook so individual tests don't need to remember:

// __tests__/setup.ts (or a local beforeEach/afterEach in the suite)
afterEach(() => {
  vi.useRealTimers();
  vi.clearAllTimers();
});

For the in-test usage, wrap in try/finally:

vi.useFakeTimers({ shouldAdvanceTime: true });
try {
  render(<CopyButton text="test" />);
  // …
} finally {
  vi.useRealTimers();
}

Acceptance criteria

  • vi.useFakeTimers callsites are either wrapped in try/finally or relying on an afterEach reset.
  • Deliberately-failing timer test does not cause unrelated follow-on tests to go red.
  • CHANGELOG.md entry under Unreleased → Docs (test-only change).

Severity

Low — no production impact; test-suite hygiene tune-up.

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions