Repository navigation
test(frontend): restore recommendations mock defaults before each test - #1391
Merged
Merged
Conversation
…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 LeanerCloud#803 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Collaborator
Author
|
Independent review (IAmJSD's agent, fresh context, given only the diff, the issue and the PR text): APPROVE Blocking:
|
Collaborator
Author
|
Local verification at
|
IAmJSD
marked this pull request as ready for review
October 24, 2026 17:30
Collaborator
Author
|
Marked ready and merged by IAmJSD's agent, at IAmJSD's request (IAmJSD reviewed the before/after screenshots; cristim is traveling), after the independent review and local verification above, all at |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
frontend/src/__tests__/recommendations.test.tsfailed 16 to 19 of its 420 tests underjest --randomize(seeds 1 to 6), even when run alone, because mock return values set by one test leaked into the next. This PR resets every mock in the file to its factory default before each test, so the file passes in any order. Test-only, no production code changes.Closes #803
What changed
frontend/src/__tests__/recommendations.test.ts(+18 lines): at file load, snapshot the factory default (getMockImplementation()) of every jest mock in the mocked../api,../stateand../api/recommendationsmodules plusmockShowToast. A new file-levelbeforeEachcallsmockReset()on each and restores that default. Describe-levelbeforeEachhooks run after it, so tests that set their own values are unaffected.Root cause:
setup.tscallsjest.clearAllMocks()before each test, which clears recorded calls but keeps anymockReturnValue/mockResolvedValue/mockImplementationa test installed. A test that set, for example,state.getCostPeriodto'hourly', a column filter, a selection, the current user or an account-override response leaked it into whichever test ran next (e.g.applyColumnFilters's'>100'savings filter then matched nothing because savings were scaled to hourly). The issue reported 6 failures; the file has grown since.Commit:
cf593904.Why this approach
It reuses the defaults already declared in the
jest.mockfactories instead of copying them into a reset list, so a default added to a factory later is restored automatically. AddingafterEachresets test by test would touch dozens of tests and miss future leaks. Switchingsetup.tsto a globaljest.resetAllMocks()would wipe the factory defaults every suite depends on. Module-level state inrecommendations.ts(expanded cells, column-filter seeding, and so on) was checked as a second possible leak; once the mock defaults were restored, no failures remained across 29 seeds, so no further resets were added.Screenshots
No visible change: test-only change.
Verification
Run on this branch's head
cf593904on top of maina14dc8f2, fromfrontend/:--randomizewith seeds 1 to 6 gave 16 to 19 failures out of 420.--randomizefor each of 29 seeds (1-26, 12345, 803, -42).npm run lint, typecheck and build pass.--maxWorkers=2): 109 suites, 3486 passed, 1 skipped. One full randomized run (seed 1510269885):recommendations.test.tspasses; 6 tests fail inusers,dashboard,riexchangeandinventory.test.ts, which have the same kind of order dependency and are outside this issue.e2e-local.sh): 157 passed.scripts/test-git-secrets-allowlist.sh: 137 PASS.Notes for review
users,dashboard,riexchangeandinventory.test.ts, so a CI-widerandomize: truewould go red. Those files need the same treatment first, which could be a follow-up issue.loadRecommendationsafterEachthat resetsgetRecommendationsColumnFilters) are now redundant but were left in to keep the diff small.Overlaps
../utilsjest.mockfactory around lines 99-110, a few lines above the new block at line 118, and may need a trivial manual merge.Note for the UI owner
This changes UI code that @cristim owns. IAmJSD reviewed the before/after screenshots and asked for it to be merged; any design point you disagree with can be a follow-up.
🤖 Generated with Claude Code