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

Show which hook timed out #7409

Open
4 tasks done
sapphi-red opened this issue Feb 3, 2025 · 0 comments
Open
4 tasks done

Show which hook timed out #7409

sapphi-red opened this issue Feb 3, 2025 · 0 comments
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@sapphi-red
Copy link
Contributor

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

@sheremet-va sheremet-va added p2-nice-to-have Not breaking anything but nice to have (priority) and removed enhancement: pending triage labels Feb 3, 2025
@sheremet-va sheremet-va moved this to P2 - 2 in Team Board Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
Status: P2 - 2
Development

No branches or pull requests

2 participants