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

Closed
4 tasks done
sapphi-red opened this issue Feb 3, 2025 · 2 comments · Fixed by #7502
Closed
4 tasks done

Show which hook timed out #7409

sapphi-red opened this issue Feb 3, 2025 · 2 comments · Fixed by #7502
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
@sheremet-va sheremet-va moved this from P2 - 2 to Approved in Team Board Feb 14, 2025
@dreamorosi
Copy link

Sorry to comment on a closed issue, but I just wanted to make sure I understand the feature before opening a new one.

I updated to vitest 3.0.9 and I see no difference in my logs when a hook times out - is there any place where this feature must be enabled? I couldn't find any indication of it in the linked PR.

@sheremet-va
Copy link
Member

Sorry to comment on a closed issue, but I just wanted to make sure I understand the feature before opening a new one.

I updated to vitest 3.0.9 and I see no difference in my logs when a hook times out - is there any place where this feature must be enabled? I couldn't find any indication of it in the linked PR.

If you have problems, open a new issue with a reproduction. Everything should work with 3.0.9.

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
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants