Skip to content

Commit

Permalink
fix: don't process config file twice (#4077)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Sep 6, 2023
1 parent c48fef5 commit a84a8e0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
13 changes: 3 additions & 10 deletions packages/vitest/src/integrations/browser/server.ts
@@ -1,25 +1,18 @@
import { createServer } from 'vite'
import { resolve } from 'pathe'
import { findUp } from 'find-up'
import { configFiles, defaultBrowserPort } from '../../constants'
import type { UserConfig } from '../../types/config'
import { defaultBrowserPort } from '../../constants'
import { ensurePackageInstalled } from '../../node/pkg'
import { resolveApiServerConfig } from '../../node/config'
import { CoverageTransform } from '../../node/plugins/coverageTransform'
import type { WorkspaceProject } from '../../node/workspace'
import { MocksPlugin } from '../../node/plugins/mocks'
import { resolveFsAllow } from '../../node/plugins/utils'

export async function createBrowserServer(project: WorkspaceProject, options: UserConfig) {
export async function createBrowserServer(project: WorkspaceProject, configFile: string | undefined) {
const root = project.config.root

await ensurePackageInstalled('@vitest/browser', root)

const configPath = options.config === false
? false
: options.config
? resolve(root, options.config)
: await findUp(configFiles, { cwd: root } as any)
const configPath = typeof configFile === 'string' ? configFile : false

const server = await createServer({
logLevel: 'error',
Expand Down
21 changes: 8 additions & 13 deletions packages/vitest/src/node/core.ts
Expand Up @@ -133,20 +133,15 @@ export class Vitest {

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

this.projects = await this.resolveWorkspace(options, cliOptions)
this.projects = await this.resolveWorkspace(cliOptions)

if (this.config.testNamePattern)
this.configOverride.testNamePattern = this.config.testNamePattern
}

private async createCoreWorkspace(options: UserConfig) {
const coreWorkspace = new WorkspaceProject(this.config.root, this)
await coreWorkspace.setServer(options, this.server, {
runner: this.runner,
server: this.vitenode,
})
this.coreWorkspaceProject = coreWorkspace
return coreWorkspace
private async createCoreProject() {
this.coreWorkspaceProject = await WorkspaceProject.createCoreProject(this)
return this.coreWorkspaceProject
}

public getCoreWorkspaceProject(): WorkspaceProject | null {
Expand All @@ -161,7 +156,7 @@ export class Vitest {
|| this.projects[0]
}

private async resolveWorkspace(options: UserConfig, cliOptions: UserConfig) {
private async resolveWorkspace(cliOptions: UserConfig) {
const configDir = this.server.config.configFile
? dirname(this.server.config.configFile)
: this.config.root
Expand All @@ -171,7 +166,7 @@ export class Vitest {
})

if (!workspaceConfigName)
return [await this.createCoreWorkspace(options)]
return [await this.createCoreProject()]

const workspaceConfigPath = join(configDir, workspaceConfigName)

Expand Down Expand Up @@ -259,7 +254,7 @@ export class Vitest {
if (
this.server.config.configFile === workspacePath
)
return this.createCoreWorkspace(options)
return this.createCoreProject()
return initializeProject(workspacePath, this, { workspaceConfigPath, test: cliOverrides })
})

Expand All @@ -268,7 +263,7 @@ export class Vitest {
})

if (!projects.length)
return [await this.createCoreWorkspace(options)]
return [await this.createCoreProject()]

const resolvedProjects = await Promise.all(projects)
const names = new Set<string>()
Expand Down
27 changes: 16 additions & 11 deletions packages/vitest/src/node/workspace.ts
Expand Up @@ -15,11 +15,6 @@ import { getBrowserProvider } from '../integrations/browser'
import { isBrowserEnabled, resolveConfig } from './config'
import { WorkspaceVitestPlugin } from './plugins/workspace'

interface InitializeServerOptions {
server?: ViteNodeServer
runner?: ViteNodeRunner
}

interface InitializeProjectOptions extends UserWorkspaceConfig {
workspaceConfigPath: string
extends?: string
Expand Down Expand Up @@ -187,20 +182,30 @@ export class WorkspaceProject {
return testFiles
}

async initBrowserServer(options: UserConfig) {
async initBrowserServer(configFile: string | undefined) {
if (!this.isBrowserEnabled())
return
await this.browser?.close()
this.browser = await createBrowserServer(this, options)
this.browser = await createBrowserServer(this, configFile)
}

static async createCoreProject(ctx: Vitest) {
const project = new WorkspaceProject(ctx.config.name || ctx.config.root, ctx)
project.vitenode = ctx.vitenode
project.server = ctx.server
project.runner = ctx.runner
project.config = ctx.config
await project.initBrowserServer(ctx.server.config.configFile)
return project
}

async setServer(options: UserConfig, server: ViteDevServer, params: InitializeServerOptions = {}) {
async setServer(options: UserConfig, server: ViteDevServer) {
this.config = resolveConfig(this.ctx.mode, options, server.config)
this.server = server

this.vitenode = params.server ?? new ViteNodeServer(server, this.config)
this.vitenode = new ViteNodeServer(server, this.config)
const node = this.vitenode
this.runner = params.runner ?? new ViteNodeRunner({
this.runner = new ViteNodeRunner({
root: server.config.root,
base: server.config.base,
fetchModule(id: string) {
Expand All @@ -211,7 +216,7 @@ export class WorkspaceProject {
},
})

await this.initBrowserServer(options)
await this.initBrowserServer(this.server.config.configFile)
}

async report<T extends keyof Reporter>(name: T, ...args: ArgumentsType<Reporter[T]>) {
Expand Down

0 comments on commit a84a8e0

Please sign in to comment.