Skip to content

onTestFinished should run after each repeat not after all the repeats are done #5869

@jackyzha0

Description

@jackyzha0

Describe the bug

I am using onTestFinished to cleanup resources per test without needing to lift variables to the describe level for afterEach.

Recently, we've started running tests with repeats to catch flakes and noticed that the run-order of onTestFinished is unexpected

test 1
afterEach 1
test 2
afterEach 2
test 3
afterEach 3
test 4
afterEach 4
onTestFinished 4
onTestFinished 3
onTestFinished 2
onTestFinished 1

whereas I would expect

test 1
afterEach 1
onTestFinished 1
test 2
afterEach 2
onTestFinished 2
test 3
afterEach 3
onTestFinished 3
test 4
afterEach 4
onTestFinished 4

Reproduction

https://replit.com/join/uueakwjekd-jzhao-replit

import { describe, expect, onTestFinished, test, afterEach } from "vitest";

describe("suite with repeats", () => {
  let n = 0;
  afterEach(() => {
    console.log('afterEach', n)
  })
  
  test("should run a test", () => {
    n += 1;
    const repeat = n;
    expect(1).toBe(1);
    onTestFinished(() => {
      console.log('onTestFinished', repeat)
    });
    
    console.log('test', repeat)
  }, { repeats: 3});
})
 RUN  v1.6.0 /home/runner/vitest-repro

stdout | index.test.ts > suite with repeats > should run a test
test 1
afterEach 1
test 2
afterEach 2
test 3
afterEach 3
test 4
afterEach 4
onTestFinished 4
onTestFinished 3
onTestFinished 2
onTestFinished 1

 ✓ index.test.ts (1)
   ✓ suite with repeats (1)
     ✓ should run a test (repeat x3)

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  19:00:54
   Duration  835ms (transform 33ms, setup 0ms, collect 24ms, tests 5ms, environment 0ms, prepare 108ms)

System Info

System:
    OS: Linux 6.5 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (16) x64 AMD EPYC 7B13
    Memory: 38.93 GB / 62.78 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 20.12.2 - /nix/store/a2iia53azgg3kjqas6cwmznpghhkkvqk-nodejs-20.12.2-wrapped/bin/node
    Yarn: 1.22.22 - /nix/store/d03xglbinz66yml6h55q6an183bxqcma-yarn-1.22.22/bin/yarn
    npm: 10.5.0 - /nix/store/a2iia53azgg3kjqas6cwmznpghhkkvqk-nodejs-20.12.2-wrapped/bin/npm
    pnpm: 8.15.5 - /nix/store/77wblnm5dnmgnan3695j3mk4r7j75s5j-pnpm-8.15.5/bin/pnpm
    bun: 1.1.11 - /nix/store/v0ps8nmsgbpdskm751vqg0s80nzkpiif-bun-1.1.11/bin/bun
  npmPackages:
    vitest: ^1.6.0 => 1.6.0 


### Used Package Manager

npm

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitest.dev/guide/).
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vitest/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions