@@ -139,7 +139,7 @@ export class Vitest {
139139 private _onRestartListeners : OnServerRestartHandler [ ] = [ ]
140140 private _onClose : ( ( ) => Awaitable < void > ) [ ] = [ ]
141141 private _onSetServer : OnServerRestartHandler [ ] = [ ]
142- private _onCancelListeners : ( ( reason : CancelReason ) => Awaitable < void > ) [ ] = [ ]
142+ private _onCancelListeners = new Set < ( reason : CancelReason ) => Awaitable < void > > ( )
143143 private _onUserTestsRerun : OnTestsRerunHandler [ ] = [ ]
144144 private _onFilterWatchedSpecification : ( ( spec : TestSpecification ) => boolean ) [ ] = [ ]
145145
@@ -706,7 +706,7 @@ export class Vitest {
706706
707707 // previous run
708708 await this . runningPromise
709- this . _onCancelListeners = [ ]
709+ this . _onCancelListeners . clear ( )
710710 this . isCancelling = false
711711
712712 // schedule the new run
@@ -807,7 +807,7 @@ export class Vitest {
807807
808808 // previous run
809809 await this . runningPromise
810- this . _onCancelListeners = [ ]
810+ this . _onCancelListeners . clear ( )
811811 this . isCancelling = false
812812
813813 // schedule the new run
@@ -859,7 +859,7 @@ export class Vitest {
859859 */
860860 async cancelCurrentRun ( reason : CancelReason ) : Promise < void > {
861861 this . isCancelling = true
862- await Promise . all ( this . _onCancelListeners . splice ( 0 ) . map ( listener => listener ( reason ) ) )
862+ await Promise . all ( [ ... this . _onCancelListeners ] . map ( listener => listener ( reason ) ) )
863863 await this . runningPromise
864864 }
865865
@@ -1257,8 +1257,11 @@ export class Vitest {
12571257 /**
12581258 * Register a handler that will be called when the test run is cancelled with `vitest.cancelCurrentRun`.
12591259 */
1260- onCancel ( fn : ( reason : CancelReason ) => Awaitable < void > ) : void {
1261- this . _onCancelListeners . push ( fn )
1260+ onCancel ( fn : ( reason : CancelReason ) => Awaitable < void > ) : ( ) => void {
1261+ this . _onCancelListeners . add ( fn )
1262+ return ( ) => {
1263+ this . _onCancelListeners . delete ( fn )
1264+ }
12621265 }
12631266
12641267 /**
0 commit comments