@@ -83,6 +83,7 @@ export class Vitest {
8383 public distPath = distDir
8484
8585 private _cachedSpecs = new Map < string , WorkspaceSpec [ ] > ( )
86+ private _workspaceConfigPath ?: string
8687
8788 /** @deprecated use `_cachedSpecs` */
8889 projectTestFiles = this . _cachedSpecs
@@ -110,6 +111,10 @@ export class Vitest {
110111 this . _browserLastPort = defaultBrowserPort
111112 this . pool ?. close ?.( )
112113 this . pool = undefined
114+ this . closingPromise = undefined
115+ this . projects = [ ]
116+ this . resolvedProjects = [ ]
117+ this . _workspaceConfigPath = undefined
113118 this . coverageProvider = undefined
114119 this . runningPromise = undefined
115120 this . _cachedSpecs . clear ( )
@@ -145,22 +150,22 @@ export class Vitest {
145150 const serverRestart = server . restart
146151 server . restart = async ( ...args ) => {
147152 await Promise . all ( this . _onRestartListeners . map ( fn => fn ( ) ) )
153+ this . report ( 'onServerRestart' )
154+ await this . close ( )
148155 await serverRestart ( ...args )
149- // watcher is recreated on restart
150- this . unregisterWatcher ( )
151- this . registerWatcher ( )
152156 }
153157
154158 // since we set `server.hmr: false`, Vite does not auto restart itself
155159 server . watcher . on ( 'change' , async ( file ) => {
156160 file = normalize ( file )
157161 const isConfig = file === server . config . configFile
162+ || this . resolvedProjects . some ( p => p . server . config . configFile === file )
163+ || file === this . _workspaceConfigPath
158164 if ( isConfig ) {
159165 await Promise . all ( this . _onRestartListeners . map ( fn => fn ( 'config' ) ) )
166+ this . report ( 'onServerRestart' , 'config' )
167+ await this . close ( )
160168 await serverRestart ( )
161- // watcher is recreated on restart
162- this . unregisterWatcher ( )
163- this . registerWatcher ( )
164169 }
165170 } )
166171 }
@@ -175,8 +180,6 @@ export class Vitest {
175180 }
176181 catch { }
177182
178- await Promise . all ( this . _onSetServer . map ( fn => fn ( ) ) )
179-
180183 const projects = await this . resolveWorkspace ( cliOptions )
181184 this . resolvedProjects = projects
182185 this . projects = projects
@@ -193,6 +196,8 @@ export class Vitest {
193196 if ( this . config . testNamePattern ) {
194197 this . configOverride . testNamePattern = this . config . testNamePattern
195198 }
199+
200+ await Promise . all ( this . _onSetServer . map ( fn => fn ( ) ) )
196201 }
197202
198203 public provide < T extends keyof ProvidedContext & string > ( key : T , value : ProvidedContext [ T ] ) {
@@ -235,7 +240,7 @@ export class Vitest {
235240 || this . projects [ 0 ]
236241 }
237242
238- private async getWorkspaceConfigPath ( ) : Promise < string | null > {
243+ private async getWorkspaceConfigPath ( ) : Promise < string | undefined > {
239244 if ( this . config . workspace ) {
240245 return this . config . workspace
241246 }
@@ -251,7 +256,7 @@ export class Vitest {
251256 } )
252257
253258 if ( ! workspaceConfigName ) {
254- return null
259+ return undefined
255260 }
256261
257262 return join ( configDir , workspaceConfigName )
@@ -260,6 +265,8 @@ export class Vitest {
260265 private async resolveWorkspace ( cliOptions : UserConfig ) {
261266 const workspaceConfigPath = await this . getWorkspaceConfigPath ( )
262267
268+ this . _workspaceConfigPath = workspaceConfigPath
269+
263270 if ( ! workspaceConfigPath ) {
264271 return [ await this . _createCoreProject ( ) ]
265272 }
0 commit comments