Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stop the runner before restarting, restart on workspace config change #6859

Merged
merged 10 commits into from
Nov 8, 2024
4 changes: 0 additions & 4 deletions packages/vitest/src/node/cli/cli-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ export async function startVitest(
stdinCleanup = registerConsoleShortcuts(ctx, stdin, stdout)
}

ctx.onServerRestart((reason) => {
ctx.report('onServerRestart', reason)
})

ctx.onAfterSetServer(() => {
if (ctx.config.standalone) {
ctx.init()
Expand Down
21 changes: 17 additions & 4 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class Vitest {
public distPath = distDir

private _cachedSpecs = new Map<string, WorkspaceSpec[]>()
private _workspaceConfigPath?: string

/** @deprecated use `_cachedSpecs` */
projectTestFiles = this._cachedSpecs
Expand Down Expand Up @@ -110,6 +111,10 @@ export class Vitest {
this._browserLastPort = defaultBrowserPort
this.pool?.close?.()
this.pool = undefined
this.closingPromise = undefined
this.projects = []
this.resolvedProjects = []
this._workspaceConfigPath = undefined
this.coverageProvider = undefined
this.runningPromise = undefined
this._cachedSpecs.clear()
Expand Down Expand Up @@ -145,6 +150,8 @@ export class Vitest {
const serverRestart = server.restart
server.restart = async (...args) => {
await Promise.all(this._onRestartListeners.map(fn => fn()))
this.report('onServerRestart', 'config')
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
await this.close()
await serverRestart(...args)
// watcher is recreated on restart
this.unregisterWatcher()
Expand All @@ -155,8 +162,12 @@ export class Vitest {
server.watcher.on('change', async (file) => {
file = normalize(file)
const isConfig = file === server.config.configFile
|| this.resolvedProjects.some(p => p.server.config.configFile === file)
|| file === this._workspaceConfigPath
if (isConfig) {
await Promise.all(this._onRestartListeners.map(fn => fn('config')))
this.report('onServerRestart', 'config')
await this.close()
await serverRestart()
// watcher is recreated on restart
this.unregisterWatcher()
Expand All @@ -175,8 +186,6 @@ export class Vitest {
}
catch { }

await Promise.all(this._onSetServer.map(fn => fn()))

const projects = await this.resolveWorkspace(cliOptions)
this.resolvedProjects = projects
this.projects = projects
Expand All @@ -193,6 +202,8 @@ export class Vitest {
if (this.config.testNamePattern) {
this.configOverride.testNamePattern = this.config.testNamePattern
}

await Promise.all(this._onSetServer.map(fn => fn()))
}

public provide<T extends keyof ProvidedContext & string>(key: T, value: ProvidedContext[T]) {
Expand Down Expand Up @@ -235,7 +246,7 @@ export class Vitest {
|| this.projects[0]
}

private async getWorkspaceConfigPath(): Promise<string | null> {
private async getWorkspaceConfigPath(): Promise<string | undefined> {
if (this.config.workspace) {
return this.config.workspace
}
Expand All @@ -251,7 +262,7 @@ export class Vitest {
})

if (!workspaceConfigName) {
return null
return undefined
}

return join(configDir, workspaceConfigName)
Expand All @@ -260,6 +271,8 @@ export class Vitest {
private async resolveWorkspace(cliOptions: UserConfig) {
const workspaceConfigPath = await this.getWorkspaceConfigPath()

this._workspaceConfigPath = workspaceConfigPath

if (!workspaceConfigPath) {
return [await this._createCoreProject()]
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ export class WorkspaceProject {
)
}

this.closingPromise = undefined
this.testProject = new TestProject(this)

this.server = server
Expand Down Expand Up @@ -476,7 +477,7 @@ export class WorkspaceProject {
if (!this.closingPromise) {
this.closingPromise = Promise.all(
[
this.server.close(),
this.server?.close(),
this.typechecker?.stop(),
this.browser?.close(),
this.clearTmpDir(),
Expand Down
4 changes: 2 additions & 2 deletions test/workspaces-browser/space_browser/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export default defineProject({
test: {
browser: {
enabled: true,
name: process.env.BROWSER || 'chrome',
name: process.env.BROWSER || 'chromium',
headless: true,
provider: process.env.PROVIDER || 'webdriverio',
provider: process.env.PROVIDER || 'playwright',
},
},
})
4 changes: 2 additions & 2 deletions test/workspaces-browser/vitest.workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export default defineWorkspace([
root: './space_browser_inline',
browser: {
enabled: true,
name: process.env.BROWSER || 'chrome',
name: process.env.BROWSER || 'chromium',
headless: true,
provider: process.env.PROVIDER || 'webdriverio',
provider: process.env.PROVIDER || 'playwright',
},
alias: {
'test-alias-from-vitest': new URL('./space_browser_inline/test-alias-to.ts', import.meta.url).pathname,
Expand Down