test: reset shared mock state so unit tests pass under --randomize - #101
Merged
Merged
Conversation
Three within-file order dependencies surfaced by `wp-scripts test-unit-js
--randomize` (e.g. seeds -1645434887, 1761699380, 1):
- edit.test.js: the mocked @wordpress/data notices store was reset only in
the "Edit focus management" block, so a leftover notice added a second
Dismiss button to the OSCOLA and soft-cap dismissal tests. Reset it in a
file-level beforeEach.
- parser.test.js: "PMID fallback resolution" resets the module registry,
so parser's lazy import('./formatting/csl') later got a fresh mock the
file's assertions never saw, and api-fetch was left un-mocked. Its
afterEach now re-points the registry at the original mock instances.
- use-citation-editor-state.test.js: race tests queue a pending
formatBibliographyEntries once-value the save never reaches, and
clearAllMocks does not drop once-queues. mockReset it in beforeEach.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #101 +/- ##
=======================================
Coverage 86.65% 86.65%
=======================================
Files 52 52
Lines 5367 5367
Branches 597 597
=======================================
Hits 4651 4651
Misses 235 235
Partials 481 481 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Summary
npx wp-scripts test-unit-js --randomize(Jest 29 shuffles tests within each file) failed onmainbecause of shared mock state. This PR resets that state where it's created. It adds no retries and pins no test order.src/edit.test.js: the mocked@wordpress/datanotices store is module-level state. Only the "Edit focus management" block reset it, so a notice left by an earlier test added a second Dismiss button. That brokeOSCOLA inspector notice › hides OSCOLA notice after dismissalandsoft-cap notice › hides warning after dismissal.__unstableResetNotices()now runs in a file-levelbeforeEach.src/lib/parser.test.js:PMID fallback resolutioncallsjest.resetModules()anddoMock. After that,parsePastedInput's lazyawait import('./formatting/csl')loaded a fresh mock that the file-levelformatBibliographyEntriesbinding never saw, so it recorded 0 calls. That brokepasses the requested style key through the formatting boundaryandcan still opt in to parser-owned formatting explicitly. The block'safterEachalso left@wordpress/api-fetchun-mocked throughdontMock. TheafterEachnow points the registry back at the original mock instances, captured withjest.requireMock.src/hooks/use-citation-editor-state.test.js: a fifth order dependence that random seeds (e.g.--seed=1) exposed. The race tests queueformatBibliographyEntries.mockReturnValueOnce(pending)but cancel before the save reaches the formatter.jest.clearAllMocks()doesn't clear Once queues, so a stale, already-resolved promise went todoes not commit when cancel fires while formatting is pending.beforeEachnow callsmockReset()before setting the implementation again.Risk: low. The change is test-only and weakens no assertions.
Verification
--randomizeon 20 seeds, including-1645434887,1761699380,1,42,12345,2024,31337,555,-424242and1000001: all green (827 passed, 2 skipped). Before this change the two reported seeds failed 4 tests, and 8 of 15 other seeds failed the hook test.npx wp-scripts lint-json the changed files: clean.npm test -- --runInBand, lint, build): passed.Related
The cross-file
--runInBandfailures (about 40 tests insave.test.js,deprecated.test.jsandparse-store-render.integration.test.js) were a separate virtual-mock leak, fixed in #100. With this branch updated tomain, the full suite passes both--runInBandand--randomize(seeds -1645434887, 1761699380, 1, 42, 777, 99999, -31337, 2026; 845 passed, 2 skipped).🤖 Generated with Claude Code