Skip to content

Commit

Permalink
fix: don't call global setup teardown twice (#3188)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Apr 13, 2023
1 parent b81ffd9 commit ba3d133
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
13 changes: 7 additions & 6 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,8 @@ export class Vitest {
return coreWorkspace
}

public getCoreWorkspaceProject() {
if (!this.coreWorkspace)
throw new Error('Core workspace project is not initialized')
return this.coreWorkspace
public getCoreWorkspaceProject(): WorkspaceProject | null {
return this.coreWorkspace || null
}

private async resolveWorkspace(options: UserConfig, cliOptions: UserConfig) {
Expand Down Expand Up @@ -687,10 +685,13 @@ export class Vitest {

async close() {
if (!this.closingPromise) {
const closePromises = this.projects.map(w => w.close())
// close the core workspace server only once
if (this.coreWorkspace && !this.projects.includes(this.coreWorkspace))
closePromises.push(this.server.close())
this.closingPromise = Promise.allSettled([
this.pool?.close(),
this.server.close(),
...this.projects.map(w => w.close()),
...closePromises,
].filter(Boolean)).then((results) => {
results.filter(r => r.status === 'rejected').forEach((err) => {
this.logger.error('error during close', (err as PromiseRejectedResult).reason)
Expand Down
5 changes: 5 additions & 0 deletions test/global-setup/globalSetup/another-vite-instance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createServer } from 'vite'
import { resolve } from 'pathe'

let teardownHappened = false

export async function setup() {
const server = await createServer({
root: resolve(__dirname, '..'),
Expand All @@ -11,6 +13,9 @@ export async function setup() {

await server.listen(9988)
return async () => {
if (teardownHappened)
throw new Error('teardown called twice')
teardownHappened = true
await server.close()
}
}
5 changes: 5 additions & 0 deletions test/global-setup/globalSetup/default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ async function sleep(n) {
return new Promise(resolve => setTimeout(resolve, n))
}

let teardownHappened = false

export default async function () {
// setup something eg start a server, db or whatever
// const server = await start()
Expand All @@ -10,6 +12,9 @@ export default async function () {
await sleep(25)

return async () => {
if (teardownHappened)
throw new Error('teardown called twice')
teardownHappened = true
// tear it down here
// await server.close()
await sleep(25)
Expand Down
5 changes: 5 additions & 0 deletions test/global-setup/globalSetup/named-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ async function sleep(n) {
return new Promise(resolve => setTimeout(resolve, n))
}

let teardownHappened = false

// let start

export async function setup() {
Expand All @@ -13,6 +15,9 @@ export async function setup() {
}

export async function teardown() {
if (teardownHappened)
throw new Error('teardown called twice')
teardownHappened = true
// tear it down here
// await server.close()
await sleep(25)
Expand Down
9 changes: 8 additions & 1 deletion test/global-setup/globalSetup/ts-with-imports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { startServer } from './server'

let teardown = false

export default async function () {
const server = await startServer('0.0.0.0', 9876)
return async () => new Promise<void>(resolve => server.close(() => resolve()))
return async () => {
if (teardown)
throw new Error('teardown called twice')
teardown = true
return new Promise<void>(resolve => server.close(() => resolve()))
}
}

0 comments on commit ba3d133

Please sign in to comment.