Skip to content
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

fix(runner): show stacktrace on hook timeout error #7502

Merged
merged 8 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 16 additions & 6 deletions packages/runner/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function withTimeout<T extends (...args: any[]) => any>(
fn: T,
timeout: number,
isHook = false,
stackTraceError?: Error,
): T {
if (timeout <= 0 || timeout === Number.POSITIVE_INFINITY) {
return fn
Expand All @@ -47,18 +48,22 @@ export function withTimeout<T extends (...args: any[]) => any>(
return new Promise((resolve_, reject_) => {
const timer = setTimeout(() => {
clearTimeout(timer)
reject(new Error(makeTimeoutMsg(isHook, timeout)))
rejectTimeoutError()
}, timeout)
// `unref` might not exist in browser
timer.unref?.()

function rejectTimeoutError() {
reject_(makeTimeoutError(isHook, timeout, stackTraceError))
}

function resolve(result: unknown) {
clearTimeout(timer)
// if test/hook took too long in microtask, setTimeout won't be triggered,
// but we still need to fail the test, see
// https://github.com/vitest-dev/vitest/issues/2920
if (now() - startTime >= timeout) {
reject_(new Error(makeTimeoutMsg(isHook, timeout)))
rejectTimeoutError()
return
}
resolve_(result)
Expand Down Expand Up @@ -108,26 +113,31 @@ export function createTestContext(
context.onTestFailed = (handler, timeout) => {
test.onFailed ||= []
test.onFailed.push(
withTimeout(handler, timeout ?? runner.config.hookTimeout, true),
withTimeout(handler, timeout ?? runner.config.hookTimeout, true, new Error('STACK_TRACE_ERROR')),
)
}

context.onTestFinished = (handler, timeout) => {
test.onFinished ||= []
test.onFinished.push(
withTimeout(handler, timeout ?? runner.config.hookTimeout, true),
withTimeout(handler, timeout ?? runner.config.hookTimeout, true, new Error('STACK_TRACE_ERROR')),
)
}

return runner.extendTaskContext?.(context) || context
}

function makeTimeoutMsg(isHook: boolean, timeout: number) {
return `${
function makeTimeoutError(isHook: boolean, timeout: number, stackTraceError?: Error) {
const message = `${
isHook ? 'Hook' : 'Test'
} timed out in ${timeout}ms.\nIf this is a long-running ${
isHook ? 'hook' : 'test'
}, pass a timeout value as the last argument or configure it globally with "${
isHook ? 'hookTimeout' : 'testTimeout'
}".`
const error = new Error(message)
if (stackTraceError?.stack) {
error.stack = stackTraceError.stack.replace(error.message, stackTraceError.message)
}
return error
}
12 changes: 6 additions & 6 deletions packages/runner/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function beforeAll(fn: BeforeAllListener, timeout?: number): void {
assertTypes(fn, '"beforeAll" callback', ['function'])
return getCurrentSuite().on(
'beforeAll',
withTimeout(fn, timeout ?? getDefaultHookTimeout(), true),
withTimeout(fn, timeout ?? getDefaultHookTimeout(), true, new Error('STACK_TRACE_ERROR')),
)
}

Expand All @@ -64,7 +64,7 @@ export function afterAll(fn: AfterAllListener, timeout?: number): void {
assertTypes(fn, '"afterAll" callback', ['function'])
return getCurrentSuite().on(
'afterAll',
withTimeout(fn, timeout ?? getDefaultHookTimeout(), true),
withTimeout(fn, timeout ?? getDefaultHookTimeout(), true, new Error('STACK_TRACE_ERROR')),
)
}

Expand Down Expand Up @@ -92,7 +92,7 @@ export function beforeEach<ExtraContext = object>(
assertTypes(fn, '"beforeEach" callback', ['function'])
return getCurrentSuite<ExtraContext>().on(
'beforeEach',
withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true),
withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true, new Error('STACK_TRACE_ERROR')),
)
}

Expand Down Expand Up @@ -120,7 +120,7 @@ export function afterEach<ExtraContext = object>(
assertTypes(fn, '"afterEach" callback', ['function'])
return getCurrentSuite<ExtraContext>().on(
'afterEach',
withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true),
withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true, new Error('STACK_TRACE_ERROR')),
)
}

Expand All @@ -147,7 +147,7 @@ export const onTestFailed: TaskHook<OnTestFailedHandler> = createTestHook(
(test, handler, timeout) => {
test.onFailed ||= []
test.onFailed.push(
withTimeout(handler, timeout ?? getDefaultHookTimeout(), true),
withTimeout(handler, timeout ?? getDefaultHookTimeout(), true, new Error('STACK_TRACE_ERROR')),
)
},
)
Expand Down Expand Up @@ -180,7 +180,7 @@ export const onTestFinished: TaskHook<OnTestFinishedHandler> = createTestHook(
(test, handler, timeout) => {
test.onFinished ||= []
test.onFinished.push(
withTimeout(handler, timeout ?? getDefaultHookTimeout(), true),
withTimeout(handler, timeout ?? getDefaultHookTimeout(), true, new Error('STACK_TRACE_ERROR')),
)
},
)
Expand Down
33 changes: 33 additions & 0 deletions test/config/fixtures/hook-timeout/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, it, beforeAll, beforeEach, afterAll, afterEach } from "vitest"

describe('beforeAll', () => {
beforeAll(async () => {
await new Promise(() => {})
}, 10)

it('ok', () => {})
})

describe('beforeEach', () => {
beforeEach(async () => {
await new Promise(() => {})
}, 20)

it('ok', () => {})
})

describe('afterAll', () => {
afterAll(async () => {
await new Promise(() => {})
}, 30)

it('ok', () => {})
})

describe('afterEach', () => {
afterEach(async () => {
await new Promise(() => {})
}, 40)

it('ok', () => {})
})
63 changes: 63 additions & 0 deletions test/config/test/__snapshots__/hook-timeout.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`timeout error with stack trace 1`] = `
"
⎯⎯⎯⎯⎯⎯ Failed Suites 2 ⎯⎯⎯⎯⎯⎯⎯

FAIL basic.test.ts > beforeAll
Error: Hook timed out in 10ms.
If this is a long-running hook, pass a timeout value as the last argument or configure it globally with "hookTimeout".
❯ basic.test.ts:4:3
2|
3| describe('beforeAll', () => {
4| beforeAll(async () => {
| ^
5| await new Promise(() => {})
6| }, 10)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/4]⎯

FAIL basic.test.ts > afterAll
Error: Hook timed out in 30ms.
If this is a long-running hook, pass a timeout value as the last argument or configure it globally with "hookTimeout".
❯ basic.test.ts:20:3
18|
19| describe('afterAll', () => {
20| afterAll(async () => {
| ^
21| await new Promise(() => {})
22| }, 30)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/4]⎯


⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯

FAIL basic.test.ts > beforeEach > ok
Error: Hook timed out in 20ms.
If this is a long-running hook, pass a timeout value as the last argument or configure it globally with "hookTimeout".
❯ basic.test.ts:12:3
10|
11| describe('beforeEach', () => {
12| beforeEach(async () => {
| ^
13| await new Promise(() => {})
14| }, 20)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/4]⎯

FAIL basic.test.ts > afterEach > ok
Error: Hook timed out in 40ms.
If this is a long-running hook, pass a timeout value as the last argument or configure it globally with "hookTimeout".
❯ basic.test.ts:28:3
26|
27| describe('afterEach', () => {
28| afterEach(async () => {
| ^
29| await new Promise(() => {})
30| }, 40)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/4]⎯

"
`;
9 changes: 9 additions & 0 deletions test/config/test/hook-timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('timeout error with stack trace', async () => {
const { stderr } = await runVitest({
root: './fixtures/hook-timeout',
})
expect(stderr).toMatchSnapshot()
})
Loading