Skip to content

Commit

Permalink
perf: close pool early in run-mode (#4623)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Nov 30, 2023
1 parent f7a7333 commit e0e2017
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ViteNodeRunner } from 'vite-node/client'
import { SnapshotManager } from '@vitest/snapshot/manager'
import type { CancelReason, File } from '@vitest/runner'
import { ViteNodeServer } from 'vite-node/server'
import type { ArgumentsType, CoverageProvider, OnServerRestartHandler, Reporter, ResolvedConfig, UserConfig, UserWorkspaceConfig, VitestRunMode } from '../types'
import type { ArgumentsType, Awaitable, CoverageProvider, OnServerRestartHandler, Reporter, ResolvedConfig, UserConfig, UserWorkspaceConfig, VitestRunMode } from '../types'
import { hasFailed, noop, slash, toArray } from '../utils'
import { getCoverageProvider } from '../integrations/coverage'
import type { BrowserProvider } from '../types/browser'
Expand Down Expand Up @@ -76,6 +76,7 @@ export class Vitest {
private _onClose: (() => Awaited<unknown>)[] = []
private _onSetServer: OnServerRestartHandler[] = []
private _onCancelListeners: ((reason: CancelReason) => Promise<void> | void)[] = []
private _poolClosePromise?: Awaitable<void>

async setServer(options: UserConfig, server: ViteDevServer, cliOptions: UserConfig) {
this.unregisterWatcher?.()
Expand Down Expand Up @@ -357,6 +358,10 @@ export class Vitest {
await this.runFiles(files)
}

// In run mode close pool as early as possible
if (!this.config.watch && this.pool?.close)
this._poolClosePromise = this.pool.close()

await this.reportCoverage(true)

if (this.config.watch)
Expand Down Expand Up @@ -780,7 +785,9 @@ export class Vitest {

if (this.pool) {
closePromises.push((async () => {
await this.pool?.close?.()
await (this._poolClosePromise || this.pool?.close?.())

this._poolClosePromise = undefined
this.pool = undefined
})())
}
Expand Down

0 comments on commit e0e2017

Please sign in to comment.