test: introduce structured test infrastructure and refactor palette tests for deterministic CI#6368
Conversation
|
✅ All Jest tests passed! This PR is ready to merge. |
|
Hi @walterbender @omsuneri , I've opened a PR introducing test infrastructure improvements and palette test refactoring. |
|
Hi @walterbender @omsuneri, |
08b863d to
96963d4
Compare
|
✅ All Jest tests passed! This PR is ready to merge. |
1 similar comment
|
✅ All Jest tests passed! This PR is ready to merge. |
5affae6 to
cd0cd4a
Compare
|
✅ All Jest tests passed! This PR is ready to merge. |
|
Rebased this PR on latest @walterbender @omsuneri could you please take a look when you have time? |
|
I like the overall approach. It seems that it could make our test infrastructure more consistent and easier to maintain. I defer to @omsuneri to hash out the details, but one question I had is why are PaletteGlobals in the utils folder? It seems specific to the palette tests themselves. |
|
@walterbender thanks for the feedback! You're right - I placed it under |
cd0cd4a to
f2bf83d
Compare
|
✅ All Jest tests passed! This PR is ready to merge. |
|
@omsuneri just a gentle ping on this when you have time. Would appreciate your thoughts, especially around the test setup structure. |
|
This PR has merge conflicts with Please rebase your branch: # Add upstream remote (one-time setup)
git remote add upstream https://github.com/sugarlabs/musicblocks.git
# Fetch latest master and rebase
git fetch upstream
git rebase upstream/master
# Resolve any conflicts, then:
git push --force-with-lease origin YOUR_BRANCH
|
f2bf83d to
32ac39f
Compare
|
Merge conflicts resolved. Ready for review. |
|
✅ All Jest tests passed! This PR is ready to merge. |
|
@omsuneri just wanted to share that I've rebased this PR on the latest master and addressed the merge conflicts. |
|
This PR has merge conflicts with Please rebase your branch: # Add upstream remote (one-time setup)
git remote add upstream https://github.com/sugarlabs/musicblocks.git
# Fetch latest master and rebase
git fetch upstream
git rebase upstream/master
# Resolve any conflicts, then:
git push --force-with-lease origin YOUR_BRANCH
|
32ac39f to
c129903
Compare
|
@vanshika2720 thanks for the review and the detailed feedback! I'll go through the suggestions, make the necessary changes, and push an update shortly |
f02c0eb to
5e81db8
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #6368 +/- ##
=======================================
Coverage 51.99% 51.99%
=======================================
Files 170 170
Lines 56871 56871
=======================================
Hits 29571 29571
Misses 27300 27300 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
- add reusable test utilities - centralize test setup
- enable setupFilesAfterEnv - use clearMocks and restoreMocks
- replace inline mocks - improve DOM handling
- use jest.spyOn instead of implicit mock
…base - ensure createDocumentFragment returns real DOM nodes - fix img.style reference in createElement mock - align mocks with jsdom behavior to prevent appendChild errors
- remove duplicate key causing ESLint failure - ensure single source of test setup configuration
- fix addEventListener spy usage in trash tests - add ErrorHandler mocks for IntervalsBlocks tests - update canvas and DOM test mocks after rebase - resolve test failures introduced during conflict resolution
Move PaletteGlobals back into palette.test.js since it is specific to the palette test suite. Also update the myblocks macro test to explicitly initialize the required macro state, making the test self-contained after the test infrastructure refactor.
Move document spies into beforeEach where needed so they are recreated after restoreMocks resets them between tests. This keeps the tests compatible with the shared test infrastructure without changing their behavior.
Rename the shared DOM helper to make it reusable outside palette tests. Update palette.test.js to use the generic helper while preserving the existing test behavior.
5e81db8 to
8f8dd70
Compare
|
I rebased the branch onto the latest upstream first to verify everything against the current codebase before addressing your suggestions. For the first point, I moved During the rebase, a few existing tests also needed small adjustments because the shared test setup now uses For the second point, I double-checked the migration from the previous Thanks again for the suggestions! |
Remove the obsolete PaletteGlobals helper after moving the palette-specific setup back into palette.test.js.
|
@vanshika2720 what do you think now? |
|
@mahesh-09-12 Nice refactoring overall. One concern replacing jest.setup.js seems to drop parts of the existing global test setup (ErrorHandler and the full canvas mock). Could we preserve the current setup or migrate it completely to avoid future test regressions? |
Migrate the remaining shared setup from jest.setup.js into test/setupTests.js. Restore the global ErrorHandler and the complete canvas mock, then remove redundant local ErrorHandler mocks and delete the obsolete jest.setup.js file.
|
Thanks @vanshika2720 for pointing this out! I completed the migration so the previous shared setup is now preserved in I reran the full test suite after these changes, and all tests are passing |
|
Hi @vanshika2720, whenever you have a chance, could you please take another look at the latest changes? I addressed your feedback and updated the PR accordingly. Thanks! |
|
@mahesh-09-12 The execution has two real problems. The setupFilesAfterEnv swap is an incomplete migration that degrades the global test environment going forward future test authors will hit missing mock methods with no indication why. The module-level spy in trash.test.js is left in place despite being the root cause of the test being fixed, and it now behaves differently from before due to restoreMocks: true being added. The second problem could cause order-dependent test failures in trash.test.js that are hard to diagnose. |
|
@vanshika2720 thanks for taking another look. I'm trying to understand these concerns. From the current revision, jest.setup.js was migrated into test/setupTests.js rather than partially replaced, and I don't see a module-level |
|
since clearMocks: true and restoreMocks: true are already configured in jest.config.js, the explicit afterEach in test/setupTests.js calling jest.clearAllMocks() and jest.restoreAllMocks() appears redundant. Unless there's a specific reason for keeping both, relying on the Jest configuration alone would simplify the test setup. |
Remove the explicit afterEach cleanup since clearMocks and restoreMocks are already configured in jest.config.js.
|
@vanshika2720 good catch! since clearMocks and restoreMocks are already enabled in jest.config.js, the explicit afterEach was redundant. I've removed it and rerun the test suite. |
|
@mahesh-09-12 I noticed after the restoreMocks: true migration is that a couple of tests still seem to rely on patterns that can leak state. The module-level window.addEventListener spy in trash.test.js appears redundant now that the resize test creates its own local spy, and the direct assignments to document.addEventListener/removeEventListener in palette.test.js won't be restored automatically by Jest. Would it make sense to remove the obsolete module-level spy and use jest.spyOn(...).mockImplementation(...) for the document listeners so cleanup is handled consistently? |
Remove the redundant module-level addEventListener spy and replace direct document listener assignments with jest.spyOn to ensure mocks are restored consistently.
|
@vanshika2720 thanks for your patience and the detailed feedback! I addressed both points by removing the redundant module-level |
Summary
This PR introduces a structured test infrastructure and refactors palette tests to improve reliability, isolation, and determinism.
The goal is to reduce flaky behavior caused by implicit globals, shared state, and loosely mocked DOM interactions — which are known issues in the current codebase and CI.
What’s Changed
1. Test Infrastructure
setupTests.js)2. Jest Configuration
setupFilesAfterEnvfor consistent test initializationrestoreMocksresetMocks(as it breaks required mock implementations)3. Palette Test Refactor
palette.test.jsto use shared utilities4. Fix: Implicit Mock Dependency
trash.test.jswhich relied on a leaked global mock ofwindow.addEventListenerjest.spyOnto ensure compatibility with restored mocksWhy This Matters
Previously:
Now:
This lays the foundation for:
Coverage Note
There is a minor drop (~1%) in functional coverage.
This is expected because:
No test cases were removed, and overall test reliability has improved.
PR Category