@@ -11,7 +11,7 @@ import type {
1111} from './types'
1212import { manageArtifactAttachment , recordArtifact , recordAsyncOperation } from './artifact'
1313import { TaskDeadline } from './deadline'
14- import { PendingError } from './errors'
14+ import { PendingError , TestRunAbortError } from './errors'
1515import { finishSendTasksUpdate } from './run'
1616import { getRunner } from './suite'
1717
@@ -116,8 +116,9 @@ export function withTimeout<T extends (...args: any[]) => any>(
116116 } as T
117117}
118118
119- export function withCancel < T extends ( ...args : any [ ] ) => any > ( fn : T , signal : AbortSignal ) : T {
119+ export function withCancel < T extends ( ...args : any [ ] ) => any > ( fn : T , context : TestContext ) : T {
120120 return function runWithCancel ( ...args : T extends ( ...args : infer A ) => any ? A : never ) {
121+ const signal = context . signal
121122 return new Promise ( ( resolve , reject ) => {
122123 const onAbort = ( ) => reject ( signal . reason )
123124 signal . addEventListener ( 'abort' , onAbort , { once : true } )
@@ -163,19 +164,22 @@ export function abortContextSignal(context: TestContext, error: Error): void {
163164 abortController ?. abort ( error )
164165}
165166
167+ export function resetContextSignal ( context : WriteableTestContext ) : void {
168+ // the next attempt of a cancelled run starts aborted
169+ if ( abortControllers . get ( context ) ?. signal . reason instanceof TestRunAbortError ) {
170+ return
171+ }
172+ const abortController = new AbortController ( )
173+ abortControllers . set ( context , abortController )
174+ context . signal = abortController . signal
175+ }
176+
166177export function createTestContext ( test : Test , runner : VitestRunner ) : TestContext {
167178 const context = function ( ) {
168179 throw new Error ( 'done() callback is deprecated, use promise instead' )
169180 } as unknown as WriteableTestContext
170181
171- let abortController = abortControllers . get ( context )
172-
173- if ( ! abortController ) {
174- abortController = new AbortController ( )
175- abortControllers . set ( context , abortController )
176- }
177-
178- context . signal = abortController . signal
182+ resetContextSignal ( context )
179183 context . task = test
180184
181185 context . skip = ( condition ?: boolean | string , note ?: string ) : never => {
@@ -237,7 +241,7 @@ export function createTestContext(test: Test, runner: VitestRunner): TestContext
237241 timeout ?? runner . config . hookTimeout ,
238242 true ,
239243 new Error ( 'STACK_TRACE_ERROR' ) ,
240- ( _ , error ) => abortController . abort ( error ) ,
244+ ( _ , error ) => abortContextSignal ( context , error ) ,
241245 ) ,
242246 )
243247 }
@@ -250,7 +254,7 @@ export function createTestContext(test: Test, runner: VitestRunner): TestContext
250254 timeout ?? runner . config . hookTimeout ,
251255 true ,
252256 new Error ( 'STACK_TRACE_ERROR' ) ,
253- ( _ , error ) => abortController . abort ( error ) ,
257+ ( _ , error ) => abortContextSignal ( context , error ) ,
254258 ) ,
255259 )
256260 }
0 commit comments