Skip to content

Commit 528bd55

Browse files
authoredJan 11, 2024
fix(vitest): call global teardown when using workspaces (#4935)
1 parent ac66481 commit 528bd55

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed
 

‎packages/vitest/src/node/core.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,11 @@ export class Vitest {
797797
async close() {
798798
if (!this.closingPromise) {
799799
this.closingPromise = (async () => {
800+
const teardownProjects = [...this.projects]
801+
if (!teardownProjects.includes(this.coreWorkspaceProject))
802+
teardownProjects.push(this.coreWorkspaceProject)
800803
// do teardown before closing the server
801-
for await (const project of [...this.projects].reverse())
804+
for await (const project of teardownProjects.reverse())
802805
await project.teardownGlobalSetup()
803806

804807
const closePromises: unknown[] = this.projects.map(w => w.close().then(() => w.server = undefined as any))

‎test/workspaces/globalTest.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ export function setup({ provide }: GlobalSetupContext) {
2424
}
2525
}
2626

27+
let teardownCalled = false
28+
2729
export async function teardown() {
30+
teardownCalled = true
2831
const results = JSON.parse(await readFile('./results.json', 'utf-8'))
2932

3033
try {
3134
assert.ok(results.success)
32-
assert.equal(results.numTotalTestSuites, 11)
33-
assert.equal(results.numTotalTests, 12)
34-
assert.equal(results.numPassedTests, 12)
35+
assert.equal(results.numTotalTestSuites, 28)
36+
assert.equal(results.numTotalTests, 29)
37+
assert.equal(results.numPassedTests, 29)
3538

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

@@ -42,3 +45,10 @@ export async function teardown() {
4245
process.exit(1)
4346
}
4447
}
48+
49+
process.on('beforeExit', () => {
50+
if (!teardownCalled) {
51+
console.error('teardown was not called')
52+
process.exitCode = 1
53+
}
54+
})

0 commit comments

Comments
 (0)
Please sign in to comment.