Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/test/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export class TestRunner {
debug('wrapping test in timeout timer')
this.#timeout = {
reject,
timer: setTimeout(() => this.#timeout!.reject(this.#createError('Test timeout')), duration),
timer: setTimeout(() => this.#timeout?.reject(this.#createError('Test timeout')), duration),
}
})
}
Expand All @@ -384,7 +384,7 @@ export class TestRunner {
debug('resetting timer')
clearTimeout(this.#timeout.timer)
this.#timeout.timer = setTimeout(
() => this.#timeout!.reject(this.#createError('Test timeout')),
() => this.#timeout?.reject(this.#createError('Test timeout')),
duration
)
}
Expand Down
32 changes: 32 additions & 0 deletions tests/test/execute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,38 @@ test.describe('execute | async', () => {
assert.equal(event!.errors[0].error.message, 'Test timeout')
assert.deepEqual(stack, [])
})

test('queued timeout callback firing after test resolution does not throw (issue #90)', async () => {
const emitter = new Emitter()
const refiner = new Refiner({})

const TIMEOUT_MS = 9_999
const originalSetTimeout = globalThis.setTimeout
const captured: Array<() => void> = []

globalThis.setTimeout = ((cb: any, ms?: number, ...args: any[]) => {
if (ms === TIMEOUT_MS && typeof cb === 'function') {
captured.push(cb as () => void)
return originalSetTimeout(() => {}, 0)
}
return originalSetTimeout(cb, ms as number, ...args)
}) as typeof setTimeout

try {
const testInstance = new Test('quick', new TestContext(), emitter, refiner)
testInstance.run(async () => {}).timeout(TIMEOUT_MS)

const [, event] = await Promise.all([testInstance.exec(), pEvent(emitter, 'test:end')])
assert.isFalse(event!.hasError)
assert.lengthOf(captured, 1)

// Simulate the race: a queued timer callback running after #clearTimer
// nulled #timeout. Before the fix this threw an unhandled TypeError.
assert.doesNotThrow(() => captured[0]())
} finally {
globalThis.setTimeout = originalSetTimeout
}
})
})

test.describe('execute | waitForDone', () => {
Expand Down
Loading