fix(core): prevent starting new run when cancelling (#8991) · vitest-dev/vitest@eb98dd8
@@ -99,6 +99,7 @@ export class Vitest {
9999/** @internal */ filenamePattern?: string[]
100100/** @internal */ runningPromise?: Promise<TestRunResult>
101101/** @internal */ closingPromise?: Promise<void>
102+/** @internal */ cancelPromise?: Promise<void | void[]>
102103/** @internal */ isCancelling = false
103104/** @internal */ coreWorkspaceProject: TestProject | undefined
104105/** @internal */ _browserSessions = new BrowserSessions()
@@ -705,6 +706,7 @@ export class Vitest {
705706await this._testRun.start(specs)
706707707708// previous run
709+await this.cancelPromise
708710await this.runningPromise
709711this._onCancelListeners.clear()
710712this.isCancelling = false
@@ -806,6 +808,7 @@ export class Vitest {
806808this.state.collectPaths(filepaths)
807809808810// previous run
811+await this.cancelPromise
809812await this.runningPromise
810813this._onCancelListeners.clear()
811814this.isCancelling = false
@@ -859,7 +862,9 @@ export class Vitest {
859862 */
860863async cancelCurrentRun(reason: CancelReason): Promise<void> {
861864this.isCancelling = true
862-await Promise.all([...this._onCancelListeners].map(listener => listener(reason)))
865+this.cancelPromise = Promise.all([...this._onCancelListeners].map(listener => listener(reason)))
866+867+await this.cancelPromise.finally(() => (this.cancelPromise = undefined))
863868await this.runningPromise
864869}
865870@@ -1045,6 +1050,7 @@ export class Vitest {
10451050private async scheduleRerun(triggerId: string): Promise<void> {
10461051const currentCount = this.restartsCount
10471052clearTimeout(this._rerunTimer)
1053+await this.cancelPromise
10481054await this.runningPromise
10491055clearTimeout(this._rerunTimer)
10501056