vitest-dev / vitest-dev/vitest
Pass `AbortSignal` instance to `beforeAll` callback
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 17.1k
- Forks
- 2k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 92
Description
Clear and concise description of the problem
I'm writing an integration test suite and I need to ensure that resources are initialized in the backend before I run the tests. I do this by polling an endpoint in the beforeAll hook.
While I think I could implement this without AbortSignal supplied by vitest it's messy because it forces me to duplicate the hook timeout value by passing it to the hook options or vitest config and to the polling function so that it stops. Having AbortSignal given by the hook itself would be much cleaner.
Suggested solution
beforeAll(async ({ signal }) => {
// ^^^^^^
await vi.waitFor(
async () => {
const resource = await fetchResource({ signal });
// ^^^^^^
if (!resource.initialized) {
throw Error(`Resource not yet initialized`);
}
},
{ signal, interval: 0 },
//^^^^^^ hypothetical, would be awesome
);
});
Alternative
const hookTimeout = 60_000;
vi.setConfig({ hookTimeout });
beforeAll(async () => {
await vi.waitFor(
async () => {
const resource = await fetchResource({ signal: undefined });
// ^^^^^^^^^^^^^^^^^ 😢
if (!resource.initialized) {
throw Error(`Resource not yet initialized`);
}
},
{ timeout: hookTimeout, interval: 0 },
);
});
Additional context
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the beforeAll hook implementation and the callback typing, then trace how hook timeouts are handled alongside vi.waitFor. Check how an AbortSignal could reach the callback and polling options, and add coverage showing cancellation when the hook times out. Done means beforeAll exposes the signal described in the issue without requiring duplicated timeout configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100