fix: avoid TypeError when timeout timer fires after test resolution (#90)#91
Merged
Merged
Conversation
Under event-loop contention the timer callback installed by TestRunner.#createTimeoutTimer / #resetTimer can be queued and dispatched by Node after #clearTimer has nulled #timeout. The callback then dereferences this.#timeout!.reject and throws "Cannot read properties of undefined (reading 'reject')" as an unhandled TypeError, failing CI even when every test passed. Replace the non-null assertion with optional chaining so the callback becomes a no-op once the timeout has been cleared, which is the correct behavior when the test has already resolved or rejected the outer promise. Adds a regression test that stubs setTimeout to capture the timer callback, lets the runner finish and clear the timer, then invokes the captured callback to deterministically reproduce the race. Closes japa#90
Collaborator
|
Thanks 👍🏽 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #90.
Problem
Under event-loop contention, the
TestRunneremits unhandledTypeError: Cannot read properties of undefined (reading 'reject')exceptions, failing CI even when every test passes.Root cause
#createTimeoutTimerand#resetTimerinstall asetTimeoutcallback that dereferencesthis.#timeout!.rejectat execution time rather than capture time. When Node dispatches a queued timer callback after the test has already resolved and#clearTimer()has setthis.#timeout = undefined, the!.non-null assertion is wrong and the callback throws.```ts
// src/test/runner.ts
timer: setTimeout(() => this.#timeout!.reject(this.#createError('Test timeout')), duration),
// ^^ #timeout may be undefined by the time this runs
```
Fix
Replace
!.with?.in both call sites. The callback safely becomes a no-op once#clearTimerhas nulled the timeout — which is the correct behavior, since the outer promise has already resolved or rejected.```diff
```
Two character changes total across
#createTimeoutTimerand#resetTimer.Test
Added a deterministic regression test in
tests/test/execute.spec.ts. It stubsglobalThis.setTimeoutto capture the timer callback (so the timer never fires naturally), runs a test that resolves immediately, then invokes the captured callback after#clearTimerhas run. Before the fix this throws the exactTypeErrorfrom the issue; after the fix it's a no-op.Verified locally:
TypeError: Cannot read properties of undefined (reading 'reject')