Skip to content

Commit

Permalink
fix: stop the runner before restarting, restart on workspace config c…
Browse files Browse the repository at this point in the history
…hange
  • Loading branch information
sheremet-va committed Nov 5, 2024
1 parent f0aeaca commit 9641b6d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/vitest/src/node/cli/cli-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
20 changes: 15 additions & 5 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,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()
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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<T extends keyof ProvidedContext & string>(key: T, value: ProvidedContext[T]) {
Expand Down Expand Up @@ -235,7 +241,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 +257,7 @@ export class Vitest {
})

if (!workspaceConfigName) {
return null
return undefined
}

return join(configDir, workspaceConfigName)
Expand All @@ -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()]
}
Expand Down Expand Up @@ -1045,7 +1053,9 @@ export class Vitest {
})
this.logger.logUpdate.done() // restore terminal cursor
})
})()
})().finally(() => {
this.closingPromise = undefined
})
}
return this.closingPromise
}
Expand Down
4 changes: 3 additions & 1 deletion packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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
2 changes: 1 addition & 1 deletion test/workspaces/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 9641b6d

Please sign in to comment.