Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

globalSetup › teardown runs twice since 0.30.0 #3181

Closed
6 tasks done
danieldiekmeier opened this issue Apr 11, 2023 · 3 comments · Fixed by #3188
Closed
6 tasks done

globalSetup › teardown runs twice since 0.30.0 #3181

danieldiekmeier opened this issue Apr 11, 2023 · 3 comments · Fixed by #3188
Assignees

Comments

@danieldiekmeier
Copy link

Describe the bug

In Vitest 0.30.0, the global teardown runs twice. I configured it like this:

// vite.config.js

export default defineConfig({
  test: {
    globalSetup: ['./setup.ts']
  }
})
// setup.ts

export function teardown() {
  console.log('teardown!');
}

In Vitest 0.29, the teardown only ran once. This was much better, because I close a Postgres connection pool in there, which actually throws an error if it gets closed twice.

I think/hope this is a bug. I hope Vitest can return to the previous behaviour!

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-searqk?file=setup.ts

  1. Run npm run test:run (should run automatically when opening the link)
  2. teardown! is printed twice

System Info

System:
    OS: macOS 13.3.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 30.25 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/.asdf/installs/nodejs/18.15.0/bin/node
    npm: 9.5.0 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chrome: 112.0.5615.49
    Edge: 112.0.1722.39
    Firefox: 103.0.2
    Firefox Developer Edition: 111.0
    Safari: 16.4
    Safari Technology Preview: 16.4
  npmPackages:
    @vitest/coverage-c8: ^0.29.8 => 0.29.8
    vite: ^4.2.1 => 4.2.1
    vitest: ^0.29.8 => 0.29.8

Used Package Manager

npm

Validations

@stackblitz
Copy link

stackblitz bot commented Apr 11, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@neo-ctataryn
Copy link

neo-ctataryn commented Apr 12, 2023

This is happening to me as well with v0.30.1. I did a console.log(new Error()) at the top of my teardown() function and here's what the stack looks like:

Error
    at Object.teardown (/home/craiger/src/myproject/test/unit/lib/vitest/global-setup.ts:38:15)
    at Context.buildEnd (file:///home/craiger/src/myproject/node_modules/vitest/dist/vendor-cli-api.70680cd5.js:12127:74)
    at hookParallel (file:///home/craiger/src/myproject/node_modules/vite/dist/node/chunks/dep-79892de8.js:42977:47)
    at Object.close (file:///home/craiger/src/myproject/node_modules/vite/dist/node/chunks/dep-79892de8.js:43422:19)
    at Object.close (file:///home/craiger/src/myproject/node_modules/vite/dist/node/chunks/dep-79892de8.js:62841:27)
    at Vitest.close (file:///home/craiger/src/myproject/node_modules/vitest/dist/vendor-cli-api.70680cd5.js:14121:21)
    at startVitest (file:///home/craiger/src/myproject/node_modules/vitest/dist/vendor-cli-api.70680cd5.js:20793:13)
    at async start (file:///home/craiger/src/myproject/node_modules/vitest/dist/cli.js:106:17)
    at async CAC.run (file:///home/craiger/src/myproject/node_modules/vitest/dist/cli.js:79:3)

AND

Error
    at Object.teardown (/home/craiger/src/myproject/test/unit/lib/vitest/global-setup.ts:38:15)
    at Context.buildEnd (file:///home/craiger/src/myproject/node_modules/vitest/dist/vendor-cli-api.70680cd5.js:12127:74)
    at hookParallel (file:///home/craiger/src/myproject/node_modules/vite/dist/node/chunks/dep-79892de8.js:42977:47)
    at Object.close (file:///home/craiger/src/myproject/node_modules/vite/dist/node/chunks/dep-79892de8.js:43422:19)
    at Object.close (file:///home/craiger/src/myproject/node_modules/vite/dist/node/chunks/dep-79892de8.js:62841:27)
    at WorkspaceProject.close (file:///home/craiger/src/myproject/node_modules/vitest/dist/vendor-cli-api.70680cd5.js:13596:21)
    at file:///home/craiger/src/myproject/node_modules/vitest/dist/vendor-cli-api.70680cd5.js:14122:39
    at Array.map (<anonymous>)
    at Vitest.close (file:///home/craiger/src/myproject/node_modules/vitest/dist/vendor-cli-api.70680cd5.js:14122:26)
    at startVitest (file:///home/craiger/src/myproject/node_modules/vitest/dist/vendor-cli-api.70680cd5.js:20793:13)
    at async start (file:///home/craiger/src/myproject/node_modules/vitest/dist/cli.js:106:17)
    at async CAC.run (file:///home/craiger/src/myproject/node_modules/vitest/dist/cli.js:79:3)
vitest teardown() called

So it looks like one of the teardown()s is being initiated from WorkspaceProject.close, whereas if I downgrade to v0.29.8 that WorkspaceProject.close invocation never happens and teardown() is only invoked once.

Hope this extra info helps.

@sheremet-va
Copy link
Member

Hope this extra info helps.

Thank you for your investigation. #3188 should fix this

@github-actions github-actions bot locked and limited conversation to collaborators Jun 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants