Skip to content

Commit

Permalink
fix!: do not exit process if global setup has failed (#5726)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored May 31, 2024
1 parent 5f71018 commit ddb09eb
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { dirname, isAbsolute, join, relative, resolve, toNamespacedPath } from '
import type { TransformResult, ViteDevServer, InlineConfig as ViteInlineConfig } from 'vite'
import { ViteNodeRunner } from 'vite-node/client'
import { ViteNodeServer } from 'vite-node/server'
import c from 'picocolors'
import { createBrowserServer } from '../integrations/browser/server'
import type { ProvidedContext, ResolvedConfig, UserConfig, UserWorkspaceConfig, Vitest } from '../types'
import type { Typechecker } from '../typecheck/typechecker'
Expand All @@ -21,7 +20,6 @@ import { WorkspaceVitestPlugin } from './plugins/workspace'
import { createViteServer } from './vite'
import type { GlobalSetupFile } from './globalSetup'
import { loadGlobalSetupFiles } from './globalSetup'
import { divider } from './reporters/renderers/utils'

interface InitializeProjectOptions extends UserWorkspaceConfig {
workspaceConfigPath: string
Expand Down Expand Up @@ -139,36 +137,21 @@ export class WorkspaceProject {

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

try {
for (const globalSetupFile of this._globalSetups) {
const teardown = await globalSetupFile.setup?.({ provide: this.provide, config: this.config })
if (teardown == null || !!globalSetupFile.teardown)
continue
if (typeof teardown !== 'function')
throw new Error(`invalid return value in globalSetup file ${globalSetupFile.file}. Must return a function`)
globalSetupFile.teardown = teardown
}
}
catch (e) {
this.logger.error(`\n${c.red(divider(c.bold(c.inverse(' Error during global setup '))))}`)
this.logger.printError(e)
process.exit(1)
for (const globalSetupFile of this._globalSetups) {
const teardown = await globalSetupFile.setup?.({ provide: this.provide, config: this.config })
if (teardown == null || !!globalSetupFile.teardown)
continue
if (typeof teardown !== 'function')
throw new Error(`invalid return value in globalSetup file ${globalSetupFile.file}. Must return a function`)
globalSetupFile.teardown = teardown
}
}

async teardownGlobalSetup() {
if (!this._globalSetups)
return
for (const globalSetupFile of [...this._globalSetups].reverse()) {
try {
await globalSetupFile.teardown?.()
}
catch (error) {
this.logger.error(`error during global teardown of ${globalSetupFile.file}`, error)
this.logger.printError(error)
process.exitCode = 1
}
}
for (const globalSetupFile of [...this._globalSetups].reverse())
await globalSetupFile.teardown?.()
}

get logger() {
Expand Down

0 comments on commit ddb09eb

Please sign in to comment.