From 9641b6dc4fbc506753f8ebff67cd7f1f3290a7e2 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 5 Nov 2024 14:39:28 +0100 Subject: [PATCH] fix: stop the runner before restarting, restart on workspace config change --- packages/vitest/src/node/cli/cli-api.ts | 3 ++- packages/vitest/src/node/core.ts | 20 ++++++++++++++----- packages/vitest/src/node/workspace.ts | 4 +++- .../space_browser/vitest.config.ts | 4 ++-- test/workspaces-browser/vitest.workspace.ts | 4 ++-- test/workspaces/vitest.config.ts | 2 +- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index 63a49f567fe1..f6b877b1c052 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -73,8 +73,9 @@ export async function startVitest( stdinCleanup = registerConsoleShortcuts(ctx, stdin, stdout) } - ctx.onServerRestart((reason) => { + ctx.onServerRestart(async (reason) => { ctx.report('onServerRestart', reason) + await ctx.close() }) ctx.onAfterSetServer(() => { diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 763fec4c468a..97111c06c354 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -83,6 +83,7 @@ export class Vitest { public distPath = distDir private _cachedSpecs = new Map() + private _workspaceConfigPath?: string /** @deprecated use `_cachedSpecs` */ projectTestFiles = this._cachedSpecs @@ -110,6 +111,9 @@ export class Vitest { this._browserLastPort = defaultBrowserPort this.pool?.close?.() this.pool = undefined + this.projects = [] + this.resolvedProjects = [] + this._workspaceConfigPath = undefined this.coverageProvider = undefined this.runningPromise = undefined this._cachedSpecs.clear() @@ -155,6 +159,8 @@ 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'))) await serverRestart() @@ -175,8 +181,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 @@ -193,6 +197,8 @@ export class Vitest { if (this.config.testNamePattern) { this.configOverride.testNamePattern = this.config.testNamePattern } + + await Promise.all(this._onSetServer.map(fn => fn())) } public provide(key: T, value: ProvidedContext[T]) { @@ -235,7 +241,7 @@ export class Vitest { || this.projects[0] } - private async getWorkspaceConfigPath(): Promise { + private async getWorkspaceConfigPath(): Promise { if (this.config.workspace) { return this.config.workspace } @@ -251,7 +257,7 @@ export class Vitest { }) if (!workspaceConfigName) { - return null + return undefined } return join(configDir, workspaceConfigName) @@ -260,6 +266,8 @@ export class Vitest { private async resolveWorkspace(cliOptions: UserConfig) { const workspaceConfigPath = await this.getWorkspaceConfigPath() + this._workspaceConfigPath = workspaceConfigPath + if (!workspaceConfigPath) { return [await this._createCoreProject()] } @@ -1045,7 +1053,9 @@ export class Vitest { }) this.logger.logUpdate.done() // restore terminal cursor }) - })() + })().finally(() => { + this.closingPromise = undefined + }) } return this.closingPromise } diff --git a/packages/vitest/src/node/workspace.ts b/packages/vitest/src/node/workspace.ts index 38135d1ff6c9..76224b58110b 100644 --- a/packages/vitest/src/node/workspace.ts +++ b/packages/vitest/src/node/workspace.ts @@ -481,7 +481,9 @@ export class WorkspaceProject { this.browser?.close(), this.clearTmpDir(), ].filter(Boolean), - ).then(() => (this._provided = {} as any)) + ).then(() => (this._provided = {} as any)).finally(() => { + this.closingPromise = undefined + }) } return this.closingPromise } diff --git a/test/workspaces-browser/space_browser/vitest.config.ts b/test/workspaces-browser/space_browser/vitest.config.ts index c3ac0296bb67..9ef18935bfc6 100644 --- a/test/workspaces-browser/space_browser/vitest.config.ts +++ b/test/workspaces-browser/space_browser/vitest.config.ts @@ -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', }, }, }) diff --git a/test/workspaces-browser/vitest.workspace.ts b/test/workspaces-browser/vitest.workspace.ts index 1bbb3f765f51..f42cf2b1256b 100644 --- a/test/workspaces-browser/vitest.workspace.ts +++ b/test/workspaces-browser/vitest.workspace.ts @@ -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, diff --git a/test/workspaces/vitest.config.ts b/test/workspaces/vitest.config.ts index d6bbb3a1a486..f0d14b87e4e3 100644 --- a/test/workspaces/vitest.config.ts +++ b/test/workspaces/vitest.config.ts @@ -2,7 +2,7 @@ import { defineConfig } from 'vitest/config' import { cwdPlugin } from './cwdPlugin.js' export default defineConfig({ - envPrefix: ['VITE_', 'CUSTOM_', 'ROOT_'], + envPrefix: ['VITE_', 'CUSTOM_', 'ROOT_', 'SS_'], plugins: [cwdPlugin('ROOT')], test: { coverage: {