Skip to content

Fix Yarn PnP ESM incompatibility and test code issues blocking test execution#15

Merged
vothanhdat merged 17 commits intomasterfrom
copilot/fix-test-flow-setup
Oct 26, 2025
Merged

Fix Yarn PnP ESM incompatibility and test code issues blocking test execution#15
vothanhdat merged 17 commits intomasterfrom
copilot/fix-test-flow-setup

Conversation

Copy link
Contributor

Copilot AI commented Oct 26, 2025

  • Examine test workflow failure on setup-testing branch
  • Identify root cause (Yarn PnP ESM resolution with jsdom/parse5)
  • Pull and merge changes from setup-testing branch to current branch
  • Implement primary fix: switch from Yarn PnP to node-modules linker
  • Update vitest config: use forks pool and increase global timeouts
  • Fix individual test timeouts in createAutoCtx.test.tsx
  • Increase waitFor timeouts from 100ms to 5000ms
  • Document complete solution in TEST_FLOW_FIX_SUMMARY.md
  • Add explicit permissions to test workflow for security
  • Fix CSS import case sensitivity in DevTool files
  • Fix remaining 5 test failures with real timer workaround
  • Update documentation to reflect 100% test success
  • Optimize test timeouts (10s → 5s) based on actual performance
  • Extract withRealTimers() helper to reduce code duplication
  • Security check passed - no vulnerabilities
  • Build successful - library builds without errors
  • All 60 tests passing (100% success rate)

Summary

Successfully fixed ALL test issues and optimized the solution based on reviewer feedback!

Phase 1 - Infrastructure Fix:

  • Before: 0/60 tests executed due to Yarn PnP ESM module resolution errors
  • Solution: Switched to node-modules linker, updated Vitest config
  • Result: 55/60 tests passing (92%)

Phase 2 - Test Code Fix:

  • Fixed 5 failing tests that hung due to fake timer/waitFor incompatibility
  • Solution: Temporarily use real timers during async assertions
  • Result: 60/60 tests passing (100%)

Phase 3 - Optimization:

  • Reduced timeouts from 10s to 5s (tests complete in ~2.5s)
  • Created withRealTimers() helper to eliminate duplication
  • All tests still pass with improved maintainability

See TEST_FLOW_FIX_SUMMARY.md for complete technical details.

Original prompt

This section details on the original issue you should resolve

<issue_title>Fix test flow failed on setup-test branch</issue_title>
<issue_description></issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

genwolff and others added 8 commits October 26, 2025 22:59
- Add Vitest, React Testing Library, and jsdom dependencies
- Create vitest.config.test.ts with coverage settings
- Add test setup with automatic cleanup
- Implement 26 passing tests for core Context system
- Add tests for ctx.ts (20 tests) covering:
  - Context class publish/subscribe
  - getContext memoization
  - useDataContext, useDataSource, useDataSubscribe hooks
  - Debouncing and multiple subscriptions
- Add tests for createRootCtx.tsx (6 tests) covering:
  - Root component creation and data provision
  - Context name derivation from props
  - useCtxState and useCtxStateStrict hooks
- Create test utilities and comprehensive documentation
- Update package.json with test scripts
- Add coverage and .vitest to .gitignore

All 26 tests passing ✅
- Test on Node 18.x and 20.x
- Run on push to master and setup-testing branches
- Run on pull requests to master
- Generate and upload coverage report to Codecov
- Use Yarn with Corepack
- Add createAutoCtx.test.tsx (12 tests) covering:
  - AutoRootCtx component rendering and lifecycle
  - Multiple subscribers with same/different params
  - Reference counting and cleanup with delays
  - Rapid mount/unmount cycles
  - Error boundary wrapping
  - State updates after auto-mounting

- Add useArrayHash.test.ts (11 tests) covering:
  - Hash generation and stability
  - Array change detection (length, elements, references)
  - Object and nested array handling
  - Large array performance
  - Mixed type handling

- Add useQuickSubscribe.test.ts (11 tests) covering:
  - Proxy-based selective subscription
  - Re-render only on accessed property changes
  - Dynamic property access
  - Object and array values
  - Multiple properties in same render
  - Memory leak prevention
  - Destructuring support

Total: 34 new tests (50/60 passing, 10 edge cases to fix)
Work in progress on edge case handling
- Update test statistics: 50/60 tests passing (83%)
- Document new test files for auto context and utility hooks
- Mark phase 2 as implemented with edge cases noted
- Update current test status section
Copilot AI and others added 6 commits October 26, 2025 18:51
- Add .yarnrc.yml to use node-modules linker instead of PnP for better test compatibility
- Change vitest pool from 'threads' to 'forks' for better ESM support
- Increase test timeouts from 500ms to 10000ms globally
- Fix individual test timeouts in createAutoCtx.test.tsx from 1000ms to 10000ms

55 out of 60 tests now pass. The 5 failing tests in createAutoCtx.test.tsx appear to have pre-existing issues with async behavior and waitFor timeouts.

Co-authored-by: vothanhdat <8221386+vothanhdat@users.noreply.github.com>
Extended waitFor timeouts from 100ms to 5000ms to allow more time for async operations.

Note: 5 tests still fail with full 10-second timeout, suggesting deeper issues with test logic rather than timing.

Co-authored-by: vothanhdat <8221386+vothanhdat@users.noreply.github.com>
Document the problem, solution, and results of fixing the Yarn PnP ESM compatibility issue that prevented tests from running.

Co-authored-by: vothanhdat <8221386+vothanhdat@users.noreply.github.com>
Set contents: read permission to follow GitHub security best practices and restrict GITHUB_TOKEN scope.

Co-authored-by: vothanhdat <8221386+vothanhdat@users.noreply.github.com>
Changed imports from './devTool.css' to './DevTool.css' to match actual filename. This was preventing the build from succeeding.

Co-authored-by: vothanhdat <8221386+vothanhdat@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix test flow failed on setup-test branch Fix Yarn PnP ESM incompatibility blocking test execution Oct 26, 2025
Copilot AI requested a review from vothanhdat October 26, 2025 19:08
Copy link
Owner

@vothanhdat vothanhdat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let fix rest test case

@vothanhdat
Copy link
Owner

Let fix rest test case

@copilot

Copilot AI and others added 2 commits October 26, 2025 19:25
- Fix renderCount scope issue in 'share instances' test by moving variable outside hook
- Add vi.useRealTimers()/vi.useFakeTimers() wrapping around waitFor calls
- Replace vi.advanceTimersByTime() with real async delays in cleanup tests
- All 60 tests now pass (100% success rate)

The issue was that fake timers prevented waitFor's internal polling from working. Tests now temporarily switch to real timers during async assertions.

Co-authored-by: vothanhdat <8221386+vothanhdat@users.noreply.github.com>
Documented the complete solution including both infrastructure fixes and test code fixes.

Co-authored-by: vothanhdat <8221386+vothanhdat@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 26, 2025

Let fix rest test case

@copilot

Fixed all remaining test cases! All 60 tests now pass (100%).

The 5 failing tests had two issues:

  1. Fake timer incompatibility: vi.useFakeTimers() prevented waitFor from polling. Fixed by wrapping async assertions with vi.useRealTimers() / vi.useFakeTimers().
  2. Scope bug: Test 1's renderCount variable was resetting on each render. Fixed by moving it outside the hook.

Commits:

Build passes ✅, security check passes ✅, all tests pass ✅

Copilot AI changed the title Fix Yarn PnP ESM incompatibility blocking test execution Fix Yarn PnP ESM incompatibility and test code issues blocking test execution Oct 26, 2025
Copilot AI requested a review from vothanhdat October 26, 2025 19:28
@vothanhdat vothanhdat requested a review from Copilot October 26, 2025 19:44
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR resolves test execution failures by addressing Yarn PnP's ESM incompatibility and fixing timing issues in test code. The changes enable all 60 tests to run and pass successfully.

Key Changes:

  • Switched from Yarn PnP to traditional node_modules to resolve ESM module resolution errors
  • Adjusted Vitest configuration for better ESM compatibility and increased timeouts
  • Fixed fake timer conflicts with async test assertions in createAutoCtx tests
  • Corrected CSS import casing and renamed utility hook for consistency

Reviewed Changes

Copilot reviewed 23 out of 25 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
.yarnrc.yml Configures traditional node_modules linker to fix ESM resolution
vitest.config.test.ts Sets up test environment with forks pool and extended timeouts
tests/createAutoCtx.test.tsx Fixes timer conflicts by switching between real/fake timers during assertions
tests/setup.ts, tests/utils.ts, tests/ctx.test.ts, tests/createRootCtx.test.tsx, tests/useQuickSubscribe.test.ts, tests/useArrayChangeId.test.ts New comprehensive test suite with proper cleanup
src/state-utils/useArrayChangeId.ts Renamed from useArrayHash with improved documentation
src/state-utils/ctx.ts Updates import and removes debug logging
src/index.ts, API_DOCUMENTATION.md, .github/copilot-instructions.md Updates references to renamed hook
src/dev-tool/*.tsx Fixes CSS import case sensitivity
package.json Adds test scripts and testing dependencies
.github/workflows/test.yml New CI workflow for automated testing

test: {
globals: true,
environment: 'jsdom',
setupFiles: ['./tests/setup.ts'],
Copy link

Copilot AI Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 10-second timeouts are significantly higher than typical test timeouts. While necessary for the current async operations, consider documenting why such long timeouts are required or optimizing test operations to reduce these values.

Suggested change
setupFiles: ['./tests/setup.ts'],
setupFiles: ['./tests/setup.ts'],
// The following timeouts (10 seconds) are intentionally set higher than typical defaults.
// This is necessary to accommodate slow async operations and environment setup delays
// caused by jsdom initialization, Yarn PnP compatibility, and the use of the forks pool.
// If test performance improves, consider reducing these values.

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to reduce timeout as fast as possible @copilot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimized timeouts from 10s to 5s. Tests complete in ~2.5s, so 5s provides a 2x safety margin while being significantly faster than 10s.

Commit: e97fdbe

- Reduce global test timeouts from 10s to 5s (tests complete in ~2.5s)
- Reduce individual test timeouts from 10s to 5s
- Add withRealTimers() helper in tests/utils.ts to eliminate code duplication
- Refactor all 5 tests using vi.useRealTimers/useFakeTimers to use the helper

This addresses reviewer feedback to optimize timeouts and reduce duplication.

Co-authored-by: vothanhdat <8221386+vothanhdat@users.noreply.github.com>
@vothanhdat vothanhdat marked this pull request as ready for review October 26, 2025 23:19
@vothanhdat vothanhdat merged commit 78706be into master Oct 26, 2025
0 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix test flow failed on setup-test branch

4 participants