vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
13.05k stars 1.17k forks source link

Vitest is stuck in a loop when using browser mode and calling `location.reload()` in `afterAll` callback #6439

Open andreatiled opened 2 months ago

andreatiled commented 2 months ago

Describe the bug

As the title describes, i'm using Vitest in browser mode and i'm calling location.reload() in afterAll callback:

afterAll(async () => {
  await myUnloadMethod()
  location.reload()
})

The reason of calling location.reload() is to ensure that after all tests in the file ran, the next tests will execute in a clean environment as if we just loaded the page

Reproduction

Can't seem to get StackBlitz work with Vitest in browser mode

System Info

System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M2 Pro
    Memory: 171.42 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.18.2 - ~/.nvm/versions/node/v18.18.2/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.18.2/bin/yarn
    npm: 9.8.1 - ~/.nvm/versions/node/v18.18.2/bin/npm
    pnpm: 8.9.2 - ~/.nvm/versions/node/v18.18.2/bin/pnpm
  Browsers:
    Brave Browser: 128.1.69.153
    Chrome: 128.0.6613.114
    Edge: 128.0.2739.54
    Firefox Nightly: 130.0a1
    Safari: 17.5
    Safari Technology Preview: 18.0
  npmPackages:
    @vitejs/plugin-basic-ssl: ^1.1.0 => 1.1.0 
    @vitest/browser: ^2.0.5 => 2.0.5 
    @vitest/coverage-istanbul: ^2.0.5 => 2.0.5 
    vite: ^5.4.2 => 5.4.2 
    vitest: ^2.0.5 => 2.0.5

Used Package Manager

npm

Validations

sheremet-va commented 2 months ago

The reason of calling location.reload() is to ensure that after all tests in the file ran, the next tests will execute in a clean environment as if we just loaded the page

What do you mean? Vitest tests run in isolated environments.

sheremet-va commented 2 months ago

So, it looks like this can only happen if the reload happens in the last test because we remove previous iframes. This also happens outside of Vitest's test lifecycle because onFinished was already called and we reported all test results. The reporter still prints test results after "FAIL"/"SUCCESS" footer, but we don't have any mechanism to report behaviour that happens after onFinished is called. I wonder if this should be resolved with the new reporter API.