Skip to content

inner aroundEach/All error shouldn't propagate to outer aroundEach/All #9669

@hi-ogawa

Description

@hi-ogawa

Describe the bug

Currently when inner aroundEach/All throws, outer aroundEach/All also get affected. This means proper outer teardown would require try/catching runTest/Suite. I don't think this is good for composability. Also when outer catches inner, inner error is not reported at all.

Here is an example:

import { test, expect, aroundEach } from 'vitest';

aroundEach(async (runTest) => {
  console.log('outer start');
  try {
    await runTest();
  } catch (e) {
    console.log('outer caught error', e);
  }
  console.log('outer end');
});

aroundEach(async (runTest) => {
  console.log('inner start');
  await runTest();
  console.log('inner end');
  throw new Error('inner error');
});

test('repro', () => {
  console.log('test start');
  expect(1).toBe(1);
  console.log('test end');
});
$ vitest

RUN  v4.1.0-beta.3 /home/projects/vitest-dev-vitest-jhjt6dyf

stdout | test/repro.test.ts > repro
🔵 outer start
🟠 inner start

stdout | test/repro.test.ts > repro
🟢 test start
🟢 test end

stdout | test/repro.test.ts > repro
🟠 inner end

stdout | test/repro.test.ts > repro
🔵 outer caught error Error: 🟠 inner error
    at /home/projects/vitest-dev-vitest-jhjt6dyf/test/repro.test.ts:17:9
    at eval (file:///home/projects/vitest-dev-vitest-jhjt6dyf/node_modules/.pnpm/@pkg.pr.new+vitest-dev+vitest+@vitest@runner@3a2f34c/node_modules/@vitest/runner/dist/index.js:2081:5)
🔵 outer end

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-jhjt6dyf?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: https://pkg.pr.new/vitest@3a2f34c => 4.1.0-beta.3

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions