@@ -377,11 +377,9 @@ export class Vitest {
377377 // populate once, update cache on watch
378378 await this . cache . stats . populateStats ( this . config . root , files )
379379
380- await this . runFiles ( files )
380+ await this . runFiles ( files , true )
381381 }
382382
383- await this . reportCoverage ( true )
384-
385383 if ( this . config . watch )
386384 await this . report ( 'onWatcherStart' )
387385 }
@@ -472,7 +470,7 @@ export class Vitest {
472470 await project . initializeGlobalSetup ( )
473471 }
474472
475- async runFiles ( paths : WorkspaceSpec [ ] ) {
473+ async runFiles ( paths : WorkspaceSpec [ ] , allTestsRun : boolean ) {
476474 const filepaths = paths . map ( ( [ , file ] ) => file )
477475 this . state . collectPaths ( filepaths )
478476
@@ -492,6 +490,10 @@ export class Vitest {
492490 this . invalidates . clear ( )
493491 this . snapshot . clear ( )
494492 this . state . clearErrors ( )
493+
494+ if ( ! this . isFirstRun && this . config . coverage . cleanOnRerun )
495+ await this . coverageProvider ?. clean ( )
496+
495497 await this . initializeGlobalSetup ( paths )
496498
497499 try {
@@ -513,7 +515,10 @@ export class Vitest {
513515 // can be duplicate files if different projects are using the same file
514516 const specs = Array . from ( new Set ( paths . map ( ( [ , p ] ) => p ) ) )
515517 await this . report ( 'onFinished' , this . state . getFiles ( specs ) , this . state . getUnhandledErrors ( ) )
518+ await this . reportCoverage ( allTestsRun )
519+
516520 this . runningPromise = undefined
521+ this . isFirstRun = false
517522 } )
518523
519524 return await this . runningPromise
@@ -530,13 +535,8 @@ export class Vitest {
530535 files = files . filter ( file => filteredFiles . some ( f => f [ 1 ] === file ) )
531536 }
532537
533- if ( this . coverageProvider && this . config . coverage . cleanOnRerun )
534- await this . coverageProvider . clean ( )
535-
536538 await this . report ( 'onWatcherRerun' , files , trigger )
537- await this . runFiles ( files . flatMap ( file => this . getProjectsByTestFile ( file ) ) )
538-
539- await this . reportCoverage ( ! trigger )
539+ await this . runFiles ( files . flatMap ( file => this . getProjectsByTestFile ( file ) ) , ! trigger )
540540
541541 await this . report ( 'onWatcherStart' , this . state . getFiles ( files ) )
542542 }
@@ -632,16 +632,11 @@ export class Vitest {
632632
633633 this . changedTests . clear ( )
634634
635- if ( this . coverageProvider && this . config . coverage . cleanOnRerun )
636- await this . coverageProvider . clean ( )
637-
638635 const triggerIds = new Set ( triggerId . map ( id => relative ( this . config . root , id ) ) )
639636 const triggerLabel = Array . from ( triggerIds ) . join ( ', ' )
640637 await this . report ( 'onWatcherRerun' , files , triggerLabel )
641638
642- await this . runFiles ( files . flatMap ( file => this . getProjectsByTestFile ( file ) ) )
643-
644- await this . reportCoverage ( false )
639+ await this . runFiles ( files . flatMap ( file => this . getProjectsByTestFile ( file ) ) , false )
645640
646641 await this . report ( 'onWatcherStart' , this . state . getFiles ( files ) )
647642 } , WATCHER_DEBOUNCE )
0 commit comments