Fix: Refactor streaming mode test to use modern async/await pattern#102
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/http/core/request.spec.ts`:
- Around line 1310-1314: The test currently calls jest.runAllTimers(); then
awaits consumePromise and finally calls jest.useRealTimers(), but if await
consumePromise throws the jest.useRealTimers() call is skipped; wrap the await
in a try/finally so jest.useRealTimers() always runs: call jest.runAllTimers(),
then try { await consumePromise; } finally { jest.useRealTimers(); }
(referencing consumePromise, jest.runAllTimers, and jest.useRealTimers to locate
the change).
- Around line 1309-1312: The test's async iterator uses setImmediate plus
Promises and currently calls jest.runAllTimers(), which can run timer callbacks
before microtasks and cause races; replace the call to jest.runAllTimers() with
await jest.runAllTimersAsync() (keeping the await before awaiting
consumePromise) so timer advances interleave microtask flushes properly — update
the invocation near the test that awaits consumePromise and references
setImmediate / consumePromise / the for await...of loop.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 558f9f95-d1b9-46fc-8cac-d3abe07d81fb
📒 Files selected for processing (1)
src/http/core/request.spec.ts
| jest.runAllTimers(); | ||
|
|
||
| await consumePromise; | ||
|
|
||
| jest.useRealTimers(); |
There was a problem hiding this comment.
jest.useRealTimers() is not called when consumePromise rejects — fake timers leak into subsequent tests.
If the expect(result).toBe('Hello World') assertion inside consumePromise fails (or any error is thrown within the IIFE), await consumePromise on Line 1312 re-throws, causing Line 1314 (jest.useRealTimers()) to be skipped entirely. Any later test in the same file will then run under fake timers, leading to silent hangs or spurious failures.
🛡️ Proposed fix: guard `useRealTimers` with `try/finally`
- // Advance timers to trigger setImmediate
- jest.runAllTimers();
-
- await consumePromise;
-
- jest.useRealTimers();
+ // Advance timers to trigger setImmediate
+ jest.runAllTimers();
+
+ try {
+ await consumePromise;
+ } finally {
+ jest.useRealTimers();
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| jest.runAllTimers(); | |
| await consumePromise; | |
| jest.useRealTimers(); | |
| jest.runAllTimers(); | |
| try { | |
| await consumePromise; | |
| } finally { | |
| jest.useRealTimers(); | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/http/core/request.spec.ts` around lines 1310 - 1314, The test currently
calls jest.runAllTimers(); then awaits consumePromise and finally calls
jest.useRealTimers(), but if await consumePromise throws the
jest.useRealTimers() call is skipped; wrap the await in a try/finally so
jest.useRealTimers() always runs: call jest.runAllTimers(), then try { await
consumePromise; } finally { jest.useRealTimers(); } (referencing consumePromise,
jest.runAllTimers, and jest.useRealTimers to locate the change).
|
@Ashwani1405 Consider the coderabbit suggestions for this since it's a test file we can take suggestion in consideration |
runAllTimersAsync Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> B test: ensure fake timers cleanup with finally
05733ac to
bb3a74e
Compare
|
Applied the CodeRabbit suggestions:
Also, squashed commits to one. |
|
LGTM, thank you |
Fixes #81
This PR refactored the streaming mode test to use async/await consistently instead of mixing done callbacks with async IIFE patterns.
Changes made:
Summary by CodeRabbit