Skip to content

Commit 9641b6d

Browse files
committed
fix: stop the runner before restarting, restart on workspace config change
1 parent f0aeaca commit 9641b6d

6 files changed

Lines changed: 25 additions & 12 deletions

File tree

packages/vitest/src/node/cli/cli-api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ export async function startVitest(
7373
stdinCleanup = registerConsoleShortcuts(ctx, stdin, stdout)
7474
}
7575

76-
ctx.onServerRestart((reason) => {
76+
ctx.onServerRestart(async (reason) => {
7777
ctx.report('onServerRestart', reason)
78+
await ctx.close()
7879
})
7980

8081
ctx.onAfterSetServer(() => {

packages/vitest/src/node/core.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,9 @@ export class Vitest {
110111
this._browserLastPort = defaultBrowserPort
111112
this.pool?.close?.()
112113
this.pool = undefined
114+
this.projects = []
115+
this.resolvedProjects = []
116+
this._workspaceConfigPath = undefined
113117
this.coverageProvider = undefined
114118
this.runningPromise = undefined
115119
this._cachedSpecs.clear()
@@ -155,6 +159,8 @@ export class Vitest {
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')))
160166
await serverRestart()
@@ -175,8 +181,6 @@ export class Vitest {
175181
}
176182
catch { }
177183

178-
await Promise.all(this._onSetServer.map(fn => fn()))
179-
180184
const projects = await this.resolveWorkspace(cliOptions)
181185
this.resolvedProjects = projects
182186
this.projects = projects
@@ -193,6 +197,8 @@ export class Vitest {
193197
if (this.config.testNamePattern) {
194198
this.configOverride.testNamePattern = this.config.testNamePattern
195199
}
200+
201+
await Promise.all(this._onSetServer.map(fn => fn()))
196202
}
197203

198204
public provide<T extends keyof ProvidedContext & string>(key: T, value: ProvidedContext[T]) {
@@ -235,7 +241,7 @@ export class Vitest {
235241
|| this.projects[0]
236242
}
237243

238-
private async getWorkspaceConfigPath(): Promise<string | null> {
244+
private async getWorkspaceConfigPath(): Promise<string | undefined> {
239245
if (this.config.workspace) {
240246
return this.config.workspace
241247
}
@@ -251,7 +257,7 @@ export class Vitest {
251257
})
252258

253259
if (!workspaceConfigName) {
254-
return null
260+
return undefined
255261
}
256262

257263
return join(configDir, workspaceConfigName)
@@ -260,6 +266,8 @@ export class Vitest {
260266
private async resolveWorkspace(cliOptions: UserConfig) {
261267
const workspaceConfigPath = await this.getWorkspaceConfigPath()
262268

269+
this._workspaceConfigPath = workspaceConfigPath
270+
263271
if (!workspaceConfigPath) {
264272
return [await this._createCoreProject()]
265273
}
@@ -1045,7 +1053,9 @@ export class Vitest {
10451053
})
10461054
this.logger.logUpdate.done() // restore terminal cursor
10471055
})
1048-
})()
1056+
})().finally(() => {
1057+
this.closingPromise = undefined
1058+
})
10491059
}
10501060
return this.closingPromise
10511061
}

packages/vitest/src/node/workspace.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,9 @@ export class WorkspaceProject {
481481
this.browser?.close(),
482482
this.clearTmpDir(),
483483
].filter(Boolean),
484-
).then(() => (this._provided = {} as any))
484+
).then(() => (this._provided = {} as any)).finally(() => {
485+
this.closingPromise = undefined
486+
})
485487
}
486488
return this.closingPromise
487489
}

test/workspaces-browser/space_browser/vitest.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ export default defineProject({
44
test: {
55
browser: {
66
enabled: true,
7-
name: process.env.BROWSER || 'chrome',
7+
name: process.env.BROWSER || 'chromium',
88
headless: true,
9-
provider: process.env.PROVIDER || 'webdriverio',
9+
provider: process.env.PROVIDER || 'playwright',
1010
},
1111
},
1212
})

test/workspaces-browser/vitest.workspace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export default defineWorkspace([
88
root: './space_browser_inline',
99
browser: {
1010
enabled: true,
11-
name: process.env.BROWSER || 'chrome',
11+
name: process.env.BROWSER || 'chromium',
1212
headless: true,
13-
provider: process.env.PROVIDER || 'webdriverio',
13+
provider: process.env.PROVIDER || 'playwright',
1414
},
1515
alias: {
1616
'test-alias-from-vitest': new URL('./space_browser_inline/test-alias-to.ts', import.meta.url).pathname,

test/workspaces/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineConfig } from 'vitest/config'
22
import { cwdPlugin } from './cwdPlugin.js'
33

44
export default defineConfig({
5-
envPrefix: ['VITE_', 'CUSTOM_', 'ROOT_'],
5+
envPrefix: ['VITE_', 'CUSTOM_', 'ROOT_', 'SS_'],
66
plugins: [cwdPlugin('ROOT')],
77
test: {
88
coverage: {

0 commit comments

Comments
 (0)