Skip to content

test: introduce structured test infrastructure and refactor palette tests for deterministic CI#6368

Open
mahesh-09-12 wants to merge 4 commits intosugarlabs:masterfrom
mahesh-09-12:test/refactor-palette-test-infra
Open

test: introduce structured test infrastructure and refactor palette tests for deterministic CI#6368
mahesh-09-12 wants to merge 4 commits intosugarlabs:masterfrom
mahesh-09-12:test/refactor-palette-test-infra

Conversation

@mahesh-09-12
Copy link
Contributor

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

  • Introduced reusable test utilities:
    • DOM mocks (docById, palette DOM factory)
    • Activity factory
    • SVG and Image mocks
  • Added centralized test setup (setupTests.js)
  • Removed duplicated inline mocks across tests

2. Jest Configuration

  • Enabled setupFilesAfterEnv for consistent test initialization
  • Used:
    • restoreMocks
  • Avoided resetMocks (as it breaks required mock implementations)

3. Palette Test Refactor

  • Migrated palette.test.js to use shared utilities
  • Replaced implicit global dependencies with controlled setup
  • Improved DOM handling using consistent mock structure
  • Stabilized macro and async-related tests

4. Fix: Implicit Mock Dependency

  • Fixed trash.test.js which relied on a leaked global mock of window.addEventListener
  • Replaced with explicit jest.spyOn to ensure compatibility with restored mocks

Why This Matters

Previously:

  • Tests relied on global state and implicit mocks
  • Fake DOM structures caused unintended execution paths
  • CI failures could differ from local runs

Now:

  • Tests are isolated and deterministic
  • Dependencies are explicit
  • Behavior is consistent across environments

This lays the foundation for:

  • Reliable regression testing
  • Safe dependency upgrades
  • Stable CI pipelines

Coverage Note

There is a minor drop (~1%) in functional coverage.

This is expected because:

  • Previous tests unintentionally executed additional code paths due to global mocks and loosely defined DOM
  • Refactored tests now only execute intended logic paths

No test cases were removed, and overall test reliability has improved.


PR Category

  • Bug Fix
  • Feature
  • Performance
  • Tests
  • Documentation

- 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
@github-actions github-actions bot added tests Adds or updates test coverage size/XL Extra large: 500-999 lines changed area/javascript Changes to JS source files area/tests Changes to test files labels Mar 23, 2026
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@mahesh-09-12
Copy link
Contributor Author

Hi @walterbender @omsuneri , I've opened a PR introducing test infrastructure improvements and palette test refactoring.
whenever you have time, I'd appreciate your feedback. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/javascript Changes to JS source files area/tests Changes to test files size/XL Extra large: 500-999 lines changed tests Adds or updates test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant