Skip to content

Race condition with aroundEach/All setup timeout errors can cause inner runTest/Suite to be called #9659

@hi-ogawa

Description

@hi-ogawa

Describe the bug

Found another edge case while digging #9653

The following example has timeout just slightly shorter than aroundEach setup, which causes setup timeout error, but still runTest to be executed, which then causes cryptic error, likely due to the test runner is tearing down at that point.

import { test, aroundEach } from 'vitest';

aroundEach(async (runTest) => {
  console.log('= aroundEach in =');
  await new Promise((r) => setTimeout(r, 11));
  await runTest();
  console.log('= aroundEach out =');
}, 10);

test('repro', () => {
  console.log('= test =');
});
❯ vitest

 DEV  v4.1.0-beta.3 /home/projects/vitest-dev-vitest-pc2y8yar

stdout | test/repro.test.ts > repro
= aroundEach in =

stdout | test/repro.test.ts
= aroundEach out =

 ❯ test/repro.test.ts (1 test | 1 failed) 18ms
   × repro 16ms

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/repro.test.ts > repro
AroundHookSetupError: The setup phase of "aroundEach" hook timed out after 10ms.
 ❯ test/repro.test.ts:3:1
      1| import { test, aroundEach } from 'vitest';
      2| 
      3| aroundEach(async (runTest) => {
       | ^
      4|   console.log('= aroundEach in =');
      5|   await new Promise((r) => setTimeout(r, 10));

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/2]⎯

 FAIL  test/repro.test.ts > repro
Error: The snapshot state for '/home/projects/vitest-dev-vitest-pc2y8yar/test/repro.test.ts' is not found. Did you call 'SnapshotClient.setup()'?
 ❯ test/repro.test.ts:6:9
      4|   console.log('= aroundEach in =');
      5|   await new Promise((r) => setTimeout(r, 10));
      6|   await runTest();
       |         ^
      7|   console.log('= aroundEach out =');
      8| }, 10);

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/2]⎯


 Test Files  1 failed (1)
      Tests  1 failed (1)
   Start at  13:04:06
   Duration  921ms (transform 308ms, setup 0ms, import 330ms, tests 18ms, environment 0ms)

 FAIL  Tests failed. Watching for file changes...
       press h to show help, press q to quit

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-pc2y8yar?file=test%2Frepro.test.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 20.19.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vitest: beta => 4.1.0-beta.3

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

Labels

p3-minor-bugAn edge case that only affects very specific usage (priority)

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions