Skip to content

Set clearMocks: true in vitest.config.js and remove manual vi.clearAllMocks() calls #412

@arasaka-net

Description

@arasaka-net

Every test file manually calls vi.clearAllMocks() in beforeEach blocks. Setting clearMocks: true in the Vitest config automates this and removes 32 lines of repeated boilerplate across 10 test files.

Context
The Vitest clearMocks option, when set to true at the config level, automatically calls vi.clearAllMocks() before each test — clearing call counts, recorded arguments, and return values on all mocks. Currently every test file in the repository includes vi.clearAllMocks() in one or more beforeEach callbacks. This is error-prone: a new test file that omits the call risks mock state leaking between tests, producing intermittent failures that are difficult to diagnose. Centralizing the setting eliminates the boilerplate and makes the default safe. Note that clearMocks resets mock state but does not restore original implementations, so existing vi.mock() module replacements are unaffected.

Affected Files

  • vitest.config.js:4 — Add clearMocks: true to the test configuration object
  • src/scripts/game.test.js — Remove vi.clearAllMocks() calls from beforeEach blocks
  • src/scripts/card.test.js — Remove vi.clearAllMocks() calls from beforeEach blocks
  • src/scripts/deck.test.js — Remove vi.clearAllMocks() calls from beforeEach blocks if present
  • src/scripts/p2p.test.js — Remove vi.clearAllMocks() calls from beforeEach blocks if present
  • src/scripts/grab.test.js — Remove vi.clearAllMocks() calls from beforeEach blocks if present
  • src/scripts/keyboard.test.js — Remove vi.clearAllMocks() calls from beforeEach blocks
  • src/scripts/token.test.js — Remove vi.clearAllMocks() calls from beforeEach blocks if present
  • src/scripts/utils.test.js — Remove vi.clearAllMocks() calls from beforeEach blocks if present
  • src/scripts/sidePanels.test.js — Remove vi.clearAllMocks() calls from beforeEach blocks
  • src/scripts/deck.integration.test.js — Remove vi.clearAllMocks() call from beforeEach block

Requirements

  • vitest.config.js includes clearMocks: true in the test object
  • All manual vi.clearAllMocks() calls in beforeEach blocks are removed from test files
  • beforeEach blocks that only contained vi.clearAllMocks() and DOM setup retain the DOM setup portion
  • All tests pass with npm test — no mock state leakage or regressions

Verification

  • npm test
  • grep -rn 'clearAllMocks' src/scripts/*.test.js | wc -l
  • grep -n 'clearMocks' vitest.config.js

Not In Scope

  • Do not add restoreMocks: true — restoring module-level mocks set via vi.mock() would break the existing test architecture
  • Do not centralize the jsdom test environment in this change (covered by issue Centralize jsdom test environment in vitest.config.js #359)
  • Do not refactor beforeEach blocks beyond removing the vi.clearAllMocks() line

Evidence

  • vitest.config.js:4 — The test config object has no clearMocks setting — defaults to false, requiring manual clearing in every test file
  • src/scripts/card.test.js:47vi.clearAllMocks() appears in a beforeEach — one of 12 such calls in this file alone
  • src/scripts/game.test.js:49vi.clearAllMocks() in beforeEach — repeated in 3 describe blocks in this file
  • src/scripts/sidePanels.test.js:45vi.clearAllMocks() in beforeEach — repeated in 3 describe blocks

Arasaka Queue Planning Division.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions