Skip to content

Commit

Permalink
fix(vitest): call global teardown when using workspaces (#4935)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 11, 2024
1 parent ac66481 commit 528bd55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/vitest/src/node/core.ts
Expand Up @@ -797,8 +797,11 @@ export class Vitest {
async close() {
if (!this.closingPromise) {
this.closingPromise = (async () => {
const teardownProjects = [...this.projects]
if (!teardownProjects.includes(this.coreWorkspaceProject))
teardownProjects.push(this.coreWorkspaceProject)
// do teardown before closing the server
for await (const project of [...this.projects].reverse())
for await (const project of teardownProjects.reverse())
await project.teardownGlobalSetup()

const closePromises: unknown[] = this.projects.map(w => w.close().then(() => w.server = undefined as any))
Expand Down
16 changes: 13 additions & 3 deletions test/workspaces/globalTest.ts
Expand Up @@ -24,14 +24,17 @@ export function setup({ provide }: GlobalSetupContext) {
}
}

let teardownCalled = false

export async function teardown() {
teardownCalled = true
const results = JSON.parse(await readFile('./results.json', 'utf-8'))

try {
assert.ok(results.success)
assert.equal(results.numTotalTestSuites, 11)
assert.equal(results.numTotalTests, 12)
assert.equal(results.numPassedTests, 12)
assert.equal(results.numTotalTestSuites, 28)
assert.equal(results.numTotalTests, 29)
assert.equal(results.numPassedTests, 29)

const shared = results.testResults.filter((r: any) => r.name.includes('space_shared/test.spec.ts'))

Expand All @@ -42,3 +45,10 @@ export async function teardown() {
process.exit(1)
}
}

process.on('beforeExit', () => {
if (!teardownCalled) {
console.error('teardown was not called')
process.exitCode = 1
}
})

0 comments on commit 528bd55

Please sign in to comment.