Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class Vitest {
/** @internal */ filenamePattern?: string[]
/** @internal */ runningPromise?: Promise<TestRunResult>
/** @internal */ closingPromise?: Promise<void>
/** @internal */ cancelPromise?: Promise<void | void[]>
/** @internal */ isCancelling = false
/** @internal */ coreWorkspaceProject: TestProject | undefined
/** @internal */ _browserSessions = new BrowserSessions()
Expand Down Expand Up @@ -705,6 +706,7 @@ export class Vitest {
await this._testRun.start(specs)

// previous run
await this.cancelPromise
await this.runningPromise
this._onCancelListeners.clear()
this.isCancelling = false
Expand Down Expand Up @@ -806,6 +808,7 @@ export class Vitest {
this.state.collectPaths(filepaths)

// previous run
await this.cancelPromise
await this.runningPromise
this._onCancelListeners.clear()
this.isCancelling = false
Expand Down Expand Up @@ -859,7 +862,9 @@ export class Vitest {
*/
async cancelCurrentRun(reason: CancelReason): Promise<void> {
this.isCancelling = true
await Promise.all([...this._onCancelListeners].map(listener => listener(reason)))
this.cancelPromise = Promise.all([...this._onCancelListeners].map(listener => listener(reason)))

await this.cancelPromise.finally(() => (this.cancelPromise = undefined))
await this.runningPromise
}

Expand Down Expand Up @@ -1045,6 +1050,7 @@ export class Vitest {
private async scheduleRerun(triggerId: string): Promise<void> {
const currentCount = this.restartsCount
clearTimeout(this._rerunTimer)
await this.cancelPromise
await this.runningPromise
clearTimeout(this._rerunTimer)

Expand Down
11 changes: 11 additions & 0 deletions test/cli/fixtures/bail-race/a.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { setTimeout } from 'node:timers/promises'
import {afterAll, expect, test } from 'vitest'


test('adds two numbers', () => {
expect(2 + 3).toBe(5)
})

test('fails adding two numbers', () => {
expect(2 + 3).toBe(6)
})
7 changes: 7 additions & 0 deletions test/cli/fixtures/bail-race/b.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'

test('does not run', () => {
throw new Error("Should never run")
})


31 changes: 31 additions & 0 deletions test/cli/test/bail-race.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { resolve } from 'pathe'
import { expect, test } from 'vitest'
import { createVitest } from 'vitest/node'
import { StableTestFileOrderSorter } from '../../test-utils'

test('cancels previous run before starting new one', async () => {
const errors: unknown[] = []

const vitest = await createVitest('test', {
maxWorkers: 1,
maxConcurrency: 1,
watch: false,
bail: 1,
root: resolve(import.meta.dirname, '../fixtures/bail-race'),
sequence: { sequencer: StableTestFileOrderSorter },
reporters: [{
onTestRunEnd(_, unhandledErrors) {
if (unhandledErrors.length) {
errors.push(...unhandledErrors)
}
},
}],
})

for (let i = 0; i <= 10; i++) {
await vitest.start()
}

// No "Error: [vitest-pool]: Cannot run tasks while pool is cancelling" errors should show up
expect(errors).toHaveLength(0)
Comment on lines +29 to +30
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

})
Loading