vitest-dev / vitest-dev/vitest

Pass `AbortSignal` instance to `beforeAll` callback

Open
#8,966 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

p2-nice-to-have
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.