From cf593904aeb625e74737ccbff0a274908042866d Mon Sep 17 00:00:00 2001 From: Astrid Gealer Date: Sat, 24 Oct 2026 01:24:04 +0000 Subject: [PATCH] test(frontend): restore recommendations.test.ts mock defaults before each test recommendations.test.ts failed 16 to 19 tests under jest --randomize (seeds 1 to 6), even when run alone. setup.ts calls jest.clearAllMocks() before each test, which clears recorded calls but keeps any mockReturnValue / mockResolvedValue / mockImplementation a test installed. So a test that set, for example, state.getCostPeriod to 'hourly', a column filter, a selection or an account override leaked that value into whichever test ran next. The default order happened to put tests after harmless overrides; random order did not. Snapshot each api, state, api/recommendations and showToast mock's factory default once at file load, and reset every mock to it in a file-level beforeEach. Describe-level beforeEach hooks still run after it, so tests that set their own values keep working. This reuses the defaults already declared in the jest.mock factories instead of repeating them. Refs #803 Co-Authored-By: Claude Opus 5.5 --- frontend/src/__tests__/recommendations.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/frontend/src/__tests__/recommendations.test.ts b/frontend/src/__tests__/recommendations.test.ts index c62b3342c..89a355817 100644 --- a/frontend/src/__tests__/recommendations.test.ts +++ b/frontend/src/__tests__/recommendations.test.ts @@ -115,6 +115,24 @@ import * as api from '../api'; import * as state from '../state'; import * as recsApi from '../api/recommendations'; +// Issue #803: setup.ts's jest.clearAllMocks() clears recorded calls but keeps +// any mockReturnValue / mockResolvedValue / mockImplementation a test +// installed, so an override (cost period, column filters, selection, user, +// account overrides, ...) leaked into whichever test jest ran next and the +// file failed under --randomize. Snapshot every module mock's factory default +// once, and restore it before each test, so each test starts from the +// defaults above whatever order the tests run in. +const mockFactoryDefaults = [...[api, state, recsApi].flatMap((mod) => Object.values(mod)), mockShowToast] + .filter((fn): fn is jest.Mock => jest.isMockFunction(fn)) + .map((fn) => [fn, fn.getMockImplementation()] as const); + +beforeEach(() => { + for (const [fn, factoryDefault] of mockFactoryDefaults) { + fn.mockReset(); + if (factoryDefault) fn.mockImplementation(factoryDefault); + } +}); + describe('Recommendations Module', () => { beforeEach(() => { // Reset DOM