-
Notifications
You must be signed in to change notification settings - Fork 1
fix(test): ensure raf hooks test assertions #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
c04d920 to
d72bfd2
Compare
There was a problem hiding this 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 fixes a critical testing issue where assertions inside setTimeout callbacks were never executed because fake timers weren't being advanced. The fix involves upgrading vitest from v2 to v3 (which provides better requestAnimationFrame support) and refactoring all useRaf* hook tests to use vi.advanceTimersToNextFrame() instead of relying on unevaluated setTimeout callbacks.
Changes:
- Upgraded vitest from v2.1.9 to v3.2.4 and @vitest/coverage-v8 accordingly
- Migrated vitest configuration from workspace-based to projects-based config
- Refactored tests for
useRafState,useRafLoop, anduseRafFnto properly execute assertions usingvi.advanceTimersToNextFrame()
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| vitest.workspace.ts | Deleted workspace configuration file |
| vitest.config.ts | Created new projects-based configuration |
| package.json | Updated vitest and @vitest/coverage-v8 to v3.2.4 |
| pnpm-lock.yaml | Updated lockfile with vitest v3 dependencies |
| use-raf-state/index.test.ts | Replaced setTimeout assertions with vi.advanceTimersToNextFrame() |
| use-raf-loop/index.test.ts | Fixed immediateCallback test bug and updated timer advancement |
| use-raf-fn/index.test.ts | Simplified tests and fixed timer advancement |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| expect(callback).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should call immediateCallback before the first frame', () => { |
Copilot
AI
Jan 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test name 'should call immediateCallback before the first frame' is misleading because immediateCallback: true is a configuration option that tells the hook to call the main callback immediately, not a separate callback. Consider renaming this test to something like 'should call callback immediately when immediateCallback option is true' to better reflect what is being tested.
| it('should call immediateCallback before the first frame', () => { | |
| it('should call callback immediately when immediateCallback option is true', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if maintainers find the name better, it can be renamed
d72bfd2 to
118002e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, LGTM.
Some tests currently pass only because the assertions inside the pending
setTimeoutcallbacks are never actually run.bug.mp4
From my understanding, assertions scheduled by
setTimeoutwhile the the timer is faked with vitest aren't actually executed until one of thevi.advanceTimer*functions are called, and everything that isn't executed by the end of the test is dropped (this is why assertion doesn't actually check anything in the fixed tests)The following test demonstrates the issue. It should fail because immediateCallback is never passed to the hook and cannot possibly be called, yet the test currently passes:
react-use/packages/react-use/src/use-raf-loop/index.test.ts
Lines 69 to 82 in 08699c0
What was changed
useRaf*hook, ensuring assertionvitestfrom v2 to v3 (necessary forrequestAnimationFramelogic testing without hacky mocks), while keeping thevitedependency untouched