diff --git a/packages/ui/rollup.config.js b/packages/ui/rollup.config.js index 527b8e7367c8..035b4b954edd 100644 --- a/packages/ui/rollup.config.js +++ b/packages/ui/rollup.config.js @@ -1,3 +1,4 @@ +import { builtinModules } from 'module' import esbuild from 'rollup-plugin-esbuild' import dts from 'rollup-plugin-dts' import resolve from '@rollup/plugin-node-resolve' @@ -11,6 +12,7 @@ const entry = [ ] const external = [ + ...builtinModules, ...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies || {}), 'worker_threads', diff --git a/packages/vite-node/rollup.config.js b/packages/vite-node/rollup.config.js index c24b4b11364e..7b1e161a8258 100644 --- a/packages/vite-node/rollup.config.js +++ b/packages/vite-node/rollup.config.js @@ -1,3 +1,4 @@ +import { builtinModules } from 'module' import esbuild from 'rollup-plugin-esbuild' import dts from 'rollup-plugin-dts' import resolve from '@rollup/plugin-node-resolve' @@ -18,6 +19,7 @@ const entries = { } const external = [ + ...builtinModules, ...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {}), 'pathe', diff --git a/packages/vitest/rollup.config.js b/packages/vitest/rollup.config.js index 91d1d15a5c5f..c8c4ef7aafbf 100644 --- a/packages/vitest/rollup.config.js +++ b/packages/vitest/rollup.config.js @@ -1,4 +1,5 @@ import fs from 'fs' +import { builtinModules } from 'module' import { dirname, join, relative, resolve } from 'pathe' import esbuild from 'rollup-plugin-esbuild' import dts from 'rollup-plugin-dts' @@ -29,6 +30,7 @@ const dtsEntries = [ ] const external = [ + ...builtinModules, ...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies), 'worker_threads', diff --git a/packages/vitest/src/node/cache/files.ts b/packages/vitest/src/node/cache/files.ts index 52b1eaa2d159..264fe80d6256 100644 --- a/packages/vitest/src/node/cache/files.ts +++ b/packages/vitest/src/node/cache/files.ts @@ -13,7 +13,6 @@ export class FilesStatsCache { public async updateStats(fsPath: string) { if (!fs.existsSync(fsPath)) return - const stats = await fs.promises.stat(fsPath) this.cache.set(fsPath, { size: stats.size }) } diff --git a/packages/vitest/src/node/cache/index.ts b/packages/vitest/src/node/cache/index.ts index 15d2fad3938c..c45b9d0a2900 100644 --- a/packages/vitest/src/node/cache/index.ts +++ b/packages/vitest/src/node/cache/index.ts @@ -5,8 +5,21 @@ import { loadConfigFromFile } from 'vite' import { configFiles } from '../../constants' import type { CliOptions } from '../cli-api' import { slash } from '../../utils' +import { FilesStatsCache } from './files' +import { ResultsCache } from './results' export class VitestCache { + results = new ResultsCache() + stats = new FilesStatsCache() + + getFileTestResults(id: string) { + return this.results.getResults(id) + } + + getFileStats(id: string) { + return this.stats.getStats(id) + } + static resolveCacheDir(root: string, dir: string | undefined) { return resolve(root, slash(dir || 'node_modules/.vitest')) } diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index fdf209b35fbf..2e62091d8fc1 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -18,6 +18,7 @@ import { StateManager } from './state' import { resolveConfig } from './config' import { printError } from './error' import { VitestGit } from './git' +import { VitestCache } from './cache' const WATCHER_DEBOUNCE = 100 const CLOSE_TIMEOUT = 1_000 @@ -29,6 +30,7 @@ export class Vitest { server: ViteDevServer = undefined! state: StateManager = undefined! snapshot: SnapshotManager = undefined! + cache: VitestCache = undefined! reporters: Reporter[] = undefined! console: Console pool: WorkerPool | undefined @@ -65,6 +67,7 @@ export class Vitest { this.server = server this.config = resolved this.state = new StateManager() + this.cache = new VitestCache() this.snapshot = new SnapshotManager({ ...resolved.snapshotOptions }) if (this.config.watch) @@ -92,12 +95,12 @@ export class Vitest { if (resolved.coverage.enabled) await cleanCoverage(resolved.coverage, resolved.coverage.clean) - this.state.results.setConfig(resolved.root, resolved.cache) + this.cache.results.setConfig(resolved.root, resolved.cache) try { - await this.state.results.readFromCache() + await this.cache.results.readFromCache() } catch (err) { - this.error(`[vitest] Error, while trying to parse cache in ${this.state.results.getCachePath()}:`, err) + this.error(`[vitest] Error, while trying to parse cache in ${this.cache.results.getCachePath()}:`, err) } } @@ -148,7 +151,7 @@ export class Vitest { } // populate once, update cache on watch - await Promise.all(files.map(file => this.state.stats.updateStats(file))) + await Promise.all(files.map(file => this.cache.stats.updateStats(file))) await this.runFiles(files) @@ -253,8 +256,8 @@ export class Vitest { await this.report('onFinished', files, this.state.getUnhandledErrors()) - this.state.results.updateResults(files) - await this.state.results.writeToCache() + this.cache.results.updateResults(files) + await this.cache.results.writeToCache() })() .finally(() => { this.runningPromise = undefined @@ -380,8 +383,8 @@ export class Vitest { if (this.state.filesMap.has(id)) { this.state.filesMap.delete(id) - this.state.results.removeFromCache(id) - this.state.stats.removeStats(id) + this.cache.results.removeFromCache(id) + this.cache.stats.removeStats(id) this.changedTests.delete(id) this.report('onTestRemoved', id) } @@ -390,7 +393,7 @@ export class Vitest { id = slash(id) if (await this.isTargetFile(id)) { this.changedTests.add(id) - await this.state.stats.updateStats(id) + await this.cache.stats.updateStats(id) this.scheduleRerun(id) } } diff --git a/packages/vitest/src/node/sequencers/BaseSequencer.ts b/packages/vitest/src/node/sequencers/BaseSequencer.ts index ce0da56735dc..68a9e648bab8 100644 --- a/packages/vitest/src/node/sequencers/BaseSequencer.ts +++ b/packages/vitest/src/node/sequencers/BaseSequencer.ts @@ -36,14 +36,14 @@ export class BaseSequencer implements TestSequencer { // async so it can be extended by other sequelizers public async sort(files: string[]): Promise { - const { state } = this.ctx + const cache = this.ctx.cache return [...files].sort((a, b) => { - const aState = state.getFileTestResults(a) - const bState = state.getFileTestResults(b) + const aState = cache.getFileTestResults(a) + const bState = cache.getFileTestResults(b) if (!aState || !bState) { - const statsA = state.getFileStats(a) - const statsB = state.getFileStats(b) + const statsA = cache.getFileStats(a) + const statsB = cache.getFileStats(b) // run unknown first if (!statsA || !statsB) diff --git a/packages/vitest/src/node/state.ts b/packages/vitest/src/node/state.ts index 72392ecd9c99..8b73db538b79 100644 --- a/packages/vitest/src/node/state.ts +++ b/packages/vitest/src/node/state.ts @@ -1,22 +1,11 @@ import type { ErrorWithDiff, File, Task, TaskResultPack, UserConsoleLog } from '../types' -import { FilesStatsCache } from './cache/files' -import { ResultsCache } from './cache/results' +// Note this file is shared for both node and browser, be aware to avoid node specific logic export class StateManager { filesMap = new Map() idMap = new Map() taskFileMap = new WeakMap() errorsSet = new Set() - results = new ResultsCache() - stats = new FilesStatsCache() - - getFileTestResults(id: string) { - return this.results.getResults(id) - } - - getFileStats(id: string) { - return this.stats.getStats(id) - } catchError(err: unknown, type: string) { (err as ErrorWithDiff).type = type diff --git a/test/core/test/sequencers.test.ts b/test/core/test/sequencers.test.ts index 745ae8e7da03..e13259ed7d86 100644 --- a/test/core/test/sequencers.test.ts +++ b/test/core/test/sequencers.test.ts @@ -8,7 +8,7 @@ const buildCtx = () => { config: { sequence: {}, }, - state: { + cache: { getFileTestResults: vi.fn(), getFileStats: vi.fn(), }, @@ -23,9 +23,9 @@ describe('base sequencer', () => { expect(sorted).toStrictEqual(files) }) - test('prioritaze unknown files', async () => { + test('prioritize unknown files', async () => { const ctx = buildCtx() - vi.spyOn(ctx.state, 'getFileStats').mockImplementation((file) => { + vi.spyOn(ctx.cache, 'getFileStats').mockImplementation((file) => { if (file === 'b') return { size: 2 } }) @@ -37,7 +37,7 @@ describe('base sequencer', () => { test('sort by size, larger first', async () => { const ctx = buildCtx() - vi.spyOn(ctx.state, 'getFileStats').mockImplementation((file) => { + vi.spyOn(ctx.cache, 'getFileStats').mockImplementation((file) => { if (file === 'a') return { size: 1 } if (file === 'b') @@ -53,7 +53,7 @@ describe('base sequencer', () => { test('sort by results, failed first', async () => { const ctx = buildCtx() - vi.spyOn(ctx.state, 'getFileTestResults').mockImplementation((file) => { + vi.spyOn(ctx.cache, 'getFileTestResults').mockImplementation((file) => { if (file === 'a') return { failed: false, duration: 1 } if (file === 'b') @@ -69,7 +69,7 @@ describe('base sequencer', () => { test('sort by results, long first', async () => { const ctx = buildCtx() - vi.spyOn(ctx.state, 'getFileTestResults').mockImplementation((file) => { + vi.spyOn(ctx.cache, 'getFileTestResults').mockImplementation((file) => { if (file === 'a') return { failed: true, duration: 1 } if (file === 'b') @@ -85,7 +85,7 @@ describe('base sequencer', () => { test('sort by results, long and failed first', async () => { const ctx = buildCtx() - vi.spyOn(ctx.state, 'getFileTestResults').mockImplementation((file) => { + vi.spyOn(ctx.cache, 'getFileTestResults').mockImplementation((file) => { if (file === 'a') return { failed: false, duration: 1 } if (file === 'b')