Skip to content

Show which hook timed out #7409

@sapphi-red

Description

@sapphi-red

Clear and concise description of the problem

Currently when the hook times out, Vitest only shows the fact that the run failed with Hook time out.

FAIL  test/basic.test.ts [ test/basic.test.ts ]
Error: Hook timed out in 1000ms.
If this is a long-running hook, pass a timeout value as the last argument or configure it globally with "hookTimeout".

Example fail: https://stackblitz.com/edit/vitest-dev-vitest-qgnj5ff5?file=test%2Fbasic.test.ts&initialPath=__vitest__/

With this error, it is difficult to know which hook timed out.

Suggested solution

Improve the error message to show the place where the hook exists like when the test failed.

FAIL  test/basic.test.ts
Error: Hook timed out in 1000ms.
If this is a long-running hook, pass a timeout value as the last argument or configure it globally with "hookTimeout".
 ❯ test/basic.test.ts:10:1
      9|
     10| beforeAll(() => {
       | ^
     11|   return new Promise((resolve) => {});
     12| });
     13|

I guess this is possible by adding new Error().stack / Error.captureStackTrace() here and passing that information to withTimeout.

assertTypes(fn, '"beforeAll" callback', ['function'])

Although, creating stacktrace for every hook might affect performance.

Alternative

Show the function name passed to the hook in the error message.

beforeAll(function myHook () {
  return new Promise((resolve) => {});
})

For the code above, the error message will be:

FAIL  test/basic.test.ts [ test/basic.test.ts ]
Error: Hook "myHook" timed out in 1000ms.
If this is a long-running hook, pass a timeout value as the last argument or configure it globally with "hookTimeout".

This is possible by passing fn.name to makeTimeoutMsg here.

reject(new Error(makeTimeoutMsg(isHook, timeout)))

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p2-nice-to-haveNot breaking anything but nice to have (priority)

    Projects

    Status

    Approved

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions