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(vitest): run globalSetup from the root config even if it's not in a workspace #4325

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ export class Vitest {
await Promise.all(this._onSetServer.map(fn => fn()))

this.projects = await this.resolveWorkspace(cliOptions)
if (!this.coreWorkspaceProject)
this.coreWorkspaceProject = WorkspaceProject.createBasicProject(this)

if (this.config.testNamePattern)
this.configOverride.testNamePattern = this.config.testNamePattern
Expand All @@ -162,8 +164,8 @@ export class Vitest {
return this.coreWorkspaceProject
}

public getCoreWorkspaceProject(): WorkspaceProject | null {
return this.coreWorkspaceProject || null
public getCoreWorkspaceProject(): WorkspaceProject {
return this.coreWorkspaceProject
}

public getProjectByTaskId(taskId: string): WorkspaceProject {
Expand Down Expand Up @@ -432,7 +434,13 @@ export class Vitest {
}

async initializeGlobalSetup(paths: WorkspaceSpec[]) {
await Promise.all(paths.map(async ([project]) => project.initializeGlobalSetup()))
const projects = new Set(paths.map(([project]) => project))
const coreProject = this.getCoreWorkspaceProject()
if (!projects.has(coreProject))
projects.add(coreProject)
await Promise.all(
Array.from(projects).map(project => project.initializeGlobalSetup()),
)
}

async runFiles(paths: WorkspaceSpec[]) {
Expand Down Expand Up @@ -745,8 +753,8 @@ export class Vitest {
const closePromises: unknown[] = this.projects.map(w => w.close().then(() => w.server = undefined as any))
// close the core workspace server only once
// it's possible that it's not initialized at all because it's not running any tests
if (!this.coreWorkspaceProject || !this.projects.includes(this.coreWorkspaceProject))
closePromises.push(this.server.close().then(() => this.server = undefined as any))
if (!this.projects.includes(this.coreWorkspaceProject))
closePromises.push(this.coreWorkspaceProject.close().then(() => this.server = undefined as any))

if (this.pool)
closePromises.push(this.pool.close().then(() => this.pool = undefined))
Expand Down
7 changes: 3 additions & 4 deletions packages/vitest/src/node/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { toArray } from '@vitest/utils'
import type { ViteNodeRunner } from 'vite-node/client'
import type { WorkspaceProject } from './workspace'

export interface GlobalSetupFile {
file: string
setup?: () => Promise<Function | void> | void
teardown?: Function
}

export async function loadGlobalSetupFiles(project: WorkspaceProject): Promise<GlobalSetupFile[]> {
const globalSetupFiles = toArray(project.server.config.test?.globalSetup)
return Promise.all(globalSetupFiles.map(file => loadGlobalSetupFile(file, project.runner)))
export async function loadGlobalSetupFiles(runner: ViteNodeRunner, globalSetup: string | string[]): Promise<GlobalSetupFile[]> {
const globalSetupFiles = toArray(globalSetup)
return Promise.all(globalSetupFiles.map(file => loadGlobalSetupFile(file, runner)))
}

async function loadGlobalSetupFile(file: string, runner: ViteNodeRunner): Promise<GlobalSetupFile> {
Expand Down
11 changes: 8 additions & 3 deletions packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class WorkspaceProject {

this._globalSetupInit = true

this._globalSetups = await loadGlobalSetupFiles(this)
this._globalSetups = await loadGlobalSetupFiles(this.runner, this.config.globalSetup)

try {
for (const globalSetupFile of this._globalSetups) {
Expand All @@ -112,7 +112,7 @@ export class WorkspaceProject {
async teardownGlobalSetup() {
if (!this._globalSetupInit || !this._globalSetups.length)
return
for (const globalSetupFile of this._globalSetups.reverse()) {
for (const globalSetupFile of [...this._globalSetups].reverse()) {
try {
await globalSetupFile.teardown?.()
}
Expand Down Expand Up @@ -246,12 +246,17 @@ export class WorkspaceProject {
this.browser = await createBrowserServer(this, configFile)
}

static async createCoreProject(ctx: Vitest) {
static createBasicProject(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
return project
}

static async createCoreProject(ctx: Vitest) {
const project = WorkspaceProject.createBasicProject(ctx)
await project.initBrowserServer(ctx.server.config.configFile)
return project
}
Expand Down
Loading