diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 0e151674264f..4d54e6b763df 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -592,38 +592,39 @@ export class Vitest { // schedule the new run this.runningPromise = (async () => { - if (!this.pool) { - this.pool = createPool(this) - } + try { + if (!this.pool) { + this.pool = createPool(this) + } - const invalidates = Array.from(this.invalidates) - this.invalidates.clear() - this.snapshot.clear() - this.state.clearErrors() + const invalidates = Array.from(this.invalidates) + this.invalidates.clear() + this.snapshot.clear() + this.state.clearErrors() - if (!this.isFirstRun && this.config.coverage.cleanOnRerun) { - await this.coverageProvider?.clean() - } + if (!this.isFirstRun && this.config.coverage.cleanOnRerun) { + await this.coverageProvider?.clean() + } - await this.initializeGlobalSetup(specs) + await this.initializeGlobalSetup(specs) - try { - await this.pool.runTests(specs as WorkspaceSpec[], invalidates) - } - catch (err) { - this.state.catchError(err, 'Unhandled Error') - } + try { + await this.pool.runTests(specs as WorkspaceSpec[], invalidates) + } + catch (err) { + this.state.catchError(err, 'Unhandled Error') + } - const files = this.state.getFiles() + const files = this.state.getFiles() - if (hasFailed(files)) { - process.exitCode = 1 - } + if (hasFailed(files)) { + process.exitCode = 1 + } - this.cache.results.updateResults(files) - await this.cache.results.writeToCache() - })() - .finally(async () => { + this.cache.results.updateResults(files) + await this.cache.results.writeToCache() + } + finally { // can be duplicate files if different projects are using the same file const files = Array.from(new Set(specs.map(spec => spec.moduleId))) const errors = this.state.getUnhandledErrors() @@ -632,7 +633,9 @@ export class Vitest { this.checkUnhandledErrors(errors) await this.report('onFinished', this.state.getFiles(files), errors, coverage) await this.reportCoverage(coverage, allTestsRun) - + } + })() + .finally(() => { this.runningPromise = undefined this.isFirstRun = false @@ -681,7 +684,7 @@ export class Vitest { process.exitCode = 1 } })() - .finally(async () => { + .finally(() => { this.runningPromise = undefined // all subsequent runs will treat this as a fresh run