fix: detect fixture that returns without calling use (#9831)#9861
Conversation
Instead of waiting for a timeout, immediately reject with a descriptive error when a fixture function returns without calling `use()`.
6d46e2a to
81a7c8a
Compare
AriPerkkio
left a comment
There was a problem hiding this comment.
Looks good to me, thanks for the fix! 💯
Thank you so much! 😊 |
sheremet-va
left a comment
There was a problem hiding this comment.
This looks good to me, but the error doesn't have a proper stack trace. It would be nice to point to the fixture declaration, but I don't know how to calculate the error trace 🤔 Does Error.captureStackTrace on the fixture function works?
I would expect something like
Error: Fixture returned without calling "use". Make sure to call "use" in every code path of the fixture function.
async ({ value }, use) => {
^
packages/runner/src/fixture.ts
Outdated
| if (!isUseFnArgResolved) { | ||
| useFnArgPromise.reject( | ||
| new Error( | ||
| 'Fixture returned without calling "use". Make sure to call "use" in every code path of the fixture function.', |
There was a problem hiding this comment.
Can it surface fixture name? For the test case, it would be setup.
EDIT: ah sorry, you've already suggested that #9861 (comment).
|
|
||
| exports[`should fail test-extend/fixture-without-destructuring.test.ts 1`] = `"FixtureParseError: The 1st argument inside a fixture must use object destructuring pattern, e.g. ({ task } => {}). Instead, received "context"."`; | ||
|
|
||
| exports[`should fail test-extend/fixture-without-use.test.ts 1`] = `"Error: Fixture returned without calling "use". Make sure to call "use" in every code path of the fixture function."`; |
There was a problem hiding this comment.
I think we should favor runInlineTests + stderr snapshot pattern, so we can view full error output (even if we don't have stack right now, it might in the future). Probably we can put it somewhere around
vitest/test/cli/test/scoped-fixtures.test.ts
Lines 22 to 54 in 8fc9cff
- Capture registration-time stack trace using Symbol-based pattern from hooks.ts - Include fixture name in error message for easier identification - Move test to runInlineTests + stderr inline snapshot pattern
|
Updated in 09b3f1c !
|
|
Thanks! |


Description
Resolves #9831
When a fixture function returns without calling
use(), Vitest currently waits for a timeout and shows a genericTest timed outerror, making it difficult to debug the root cause.This PR detects the case immediately and rejects with a descriptive error:
Implementation
.then().catch()chain inresolveFixtureFunction—.then()detects when the fixture resolves withoutuse()being called,.catch()handles errors thrown inside the fixture (existing behavior).Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand. No documentation needed — this only improves an error message.Changesets
feat:,fix:,perf:,docs:, orchore:.