diff --git a/packages/browser/rollup.config.js b/packages/browser/rollup.config.js index 82eb3a945..5286fac12 100644 --- a/packages/browser/rollup.config.js +++ b/packages/browser/rollup.config.js @@ -13,6 +13,7 @@ const external = [ ...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies || {}), /^@?vitest(\/|$)/, + '@vitest/browser/utils', 'worker_threads', 'node:worker_threads', 'vite', @@ -98,6 +99,7 @@ export default () => file: 'dist/context.js', format: 'esm', }, + external: ['@vitest/browser/utils'], plugins: [ oxc({ transform: { target: 'node18' }, diff --git a/packages/browser/src/client/tester/logger.ts b/packages/browser/src/client/tester/logger.ts index 51d760222..88ebb4cef 100644 --- a/packages/browser/src/client/tester/logger.ts +++ b/packages/browser/src/client/tester/logger.ts @@ -1,4 +1,4 @@ -import { format, stringify } from 'vitest/utils' +import { format, stringify } from 'vitest/internal/browser' import { getConfig } from '../utils' import { rpc } from './rpc' import { getBrowserRunner } from './runner' diff --git a/packages/browser/src/client/tester/public-utils.ts b/packages/browser/src/client/tester/public-utils.ts index 3626c83e3..241638f73 100644 --- a/packages/browser/src/client/tester/public-utils.ts +++ b/packages/browser/src/client/tester/public-utils.ts @@ -1,8 +1,8 @@ import type { Locator, LocatorSelectors } from '@vitest/browser/context' -import type { StringifyOptions } from 'vitest/utils' +import type { StringifyOptions } from 'vitest/internal/browser' import { page } from '@vitest/browser/context' import { asLocator } from 'ivya' -import { stringify } from 'vitest/utils' +import { stringify } from 'vitest/internal/browser' export function getElementLocatorSelectors(element: Element): LocatorSelectors { const locator = page.elementLocator(element) diff --git a/packages/browser/src/client/tester/rpc.ts b/packages/browser/src/client/tester/rpc.ts index ff3e1f413..cbf6ec119 100644 --- a/packages/browser/src/client/tester/rpc.ts +++ b/packages/browser/src/client/tester/rpc.ts @@ -1,5 +1,5 @@ import type { VitestBrowserClient } from '@vitest/browser/client' -import { getSafeTimers } from 'vitest/utils' +import { getSafeTimers } from 'vitest/internal/browser' const { get } = Reflect diff --git a/packages/browser/src/client/tester/runner.ts b/packages/browser/src/client/tester/runner.ts index a877aad77..07da626e3 100644 --- a/packages/browser/src/client/tester/runner.ts +++ b/packages/browser/src/client/tester/runner.ts @@ -4,9 +4,14 @@ import type { VitestExecutor } from 'vitest/execute' import type { VitestBrowserClientMocker } from './mocker' import { globalChannel, onCancel } from '@vitest/browser/client' import { page, userEvent } from '@vitest/browser/context' -import { loadDiffConfig, loadSnapshotSerializers, takeCoverageInsideWorker } from 'vitest/browser' +import { + loadDiffConfig, + loadSnapshotSerializers, + originalPositionFor, + takeCoverageInsideWorker, + TraceMap, +} from 'vitest/internal/browser' import { NodeBenchmarkRunner, VitestTestRunner } from 'vitest/runners' -import { originalPositionFor, TraceMap } from 'vitest/utils' import { createStackString, parseStacktrace } from '../../../../utils/src/source-map' import { executor, getWorkerState } from '../utils' import { rpc } from './rpc' diff --git a/packages/browser/src/client/tester/snapshot.ts b/packages/browser/src/client/tester/snapshot.ts index b0785a6d6..e2042df84 100644 --- a/packages/browser/src/client/tester/snapshot.ts +++ b/packages/browser/src/client/tester/snapshot.ts @@ -1,7 +1,7 @@ import type { VitestBrowserClient } from '@vitest/browser/client' +import type { ParsedStack } from 'vitest/internal/browser' import type { SnapshotEnvironment } from 'vitest/snapshot' -import type { ParsedStack } from 'vitest/utils' -import { originalPositionFor, TraceMap } from 'vitest/utils' +import { originalPositionFor, TraceMap } from 'vitest/internal/browser' export class VitestBrowserSnapshotEnvironment implements SnapshotEnvironment { private sourceMaps = new Map() diff --git a/packages/browser/src/client/tester/tester.ts b/packages/browser/src/client/tester/tester.ts index 44e7abb97..a7ec2a805 100644 --- a/packages/browser/src/client/tester/tester.ts +++ b/packages/browser/src/client/tester/tester.ts @@ -9,7 +9,7 @@ import { startCoverageInsideWorker, startTests, stopCoverageInsideWorker, -} from 'vitest/browser' +} from 'vitest/internal/browser' import { executor, getBrowserState, getConfig, getWorkerState } from '../utils' import { setupDialogsSpy } from './dialog' import { setupConsoleLogSpy } from './logger' diff --git a/packages/browser/src/node/plugin.ts b/packages/browser/src/node/plugin.ts index d1f75d3a9..987785ce0 100644 --- a/packages/browser/src/node/plugin.ts +++ b/packages/browser/src/node/plugin.ts @@ -190,8 +190,7 @@ export default (parentServer: ParentBrowserProject, base = '/'): Plugin[] => { const exclude = [ 'vitest', - 'vitest/utils', - 'vitest/browser', + 'vitest/internal/browser', 'vitest/runners', '@vitest/browser', '@vitest/browser/client', diff --git a/packages/browser/utils.d.ts b/packages/browser/utils.d.ts index 9a78b236f..cd62022af 100644 --- a/packages/browser/utils.d.ts +++ b/packages/browser/utils.d.ts @@ -3,7 +3,7 @@ // fortunately, the file is quite small import { LocatorSelectors, Locator } from '@vitest/browser/context' -import { StringifyOptions } from 'vitest/utils' +import { StringifyOptions } from 'vitest/internal/browser' export type PrettyDOMOptions = Omit diff --git a/packages/utils/src/display.ts b/packages/utils/src/display.ts index a538d6a9e..d374966d2 100644 --- a/packages/utils/src/display.ts +++ b/packages/utils/src/display.ts @@ -21,7 +21,7 @@ interface Options { stylize: (value: string, styleType: string) => string } -type LoupeOptions = Partial +export type LoupeOptions = Partial const { AsymmetricMatcher, diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index cdf049c5a..d7d9c9d18 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -4,7 +4,7 @@ export { objDisplay, stringify, } from './display' -export type { StringifyOptions } from './display' +export type { LoupeOptions, StringifyOptions } from './display' export { assertTypes, diff --git a/packages/vitest/package.json b/packages/vitest/package.json index 41fdcf2f9..22b61b8a1 100644 --- a/packages/vitest/package.json +++ b/packages/vitest/package.json @@ -58,7 +58,7 @@ "types": "./dist/workers.d.ts", "import": "./dist/workers.js" }, - "./browser": { + "./internal/browser": { "types": "./dist/browser.d.ts", "default": "./dist/browser.js" }, @@ -74,10 +74,6 @@ "types": "./dist/environments.d.ts", "default": "./dist/environments.js" }, - "./utils": { - "types": "./dist/utils.d.ts", - "default": "./dist/utils.js" - }, "./config": { "types": "./config.d.ts", "require": "./dist/config.cjs", diff --git a/packages/vitest/rollup.config.js b/packages/vitest/rollup.config.js index f2ad94aff..43859fd8f 100644 --- a/packages/vitest/rollup.config.js +++ b/packages/vitest/rollup.config.js @@ -28,7 +28,6 @@ const entries = { 'mocker': 'src/public/mocker.ts', 'spy': 'src/integrations/spy.ts', 'coverage': 'src/public/coverage.ts', - 'utils': 'src/public/utils.ts', 'execute': 'src/public/execute.ts', 'reporters': 'src/public/reporters.ts', // TODO: advanced docs @@ -55,7 +54,6 @@ const dtsEntries = { suite: 'src/public/suite.ts', config: 'src/public/config.ts', coverage: 'src/public/coverage.ts', - utils: 'src/public/utils.ts', execute: 'src/public/execute.ts', reporters: 'src/public/reporters.ts', mocker: 'src/public/mocker.ts', diff --git a/packages/vitest/src/integrations/run-once.ts b/packages/vitest/src/integrations/run-once.ts deleted file mode 100644 index cb22304d3..000000000 --- a/packages/vitest/src/integrations/run-once.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { getWorkerState } from '../runtime/utils' - -const filesCount = new Map() -const cache = new Map() - -/** - * This utils allows computational intensive tasks to only be ran once - * across test reruns to improve the watch mode performance. - * - * Currently only works with `poolOptions..isolate: false` - * - * @experimental - */ -export function runOnce(fn: () => T, key?: string): T { - const filepath = getWorkerState().filepath || '__unknown_files__' - - if (!key) { - filesCount.set(filepath, (filesCount.get(filepath) || 0) + 1) - key = String(filesCount.get(filepath)!) - } - - const id = `${filepath}:${key}` - - if (!cache.has(id)) { - cache.set(id, fn()) - } - - return cache.get(id) -} - -/** - * Get a boolean indicates whether the task is running in the first time. - * Could only be `false` in watch mode. - * - * Currently only works with `isolate: false` - * - * @experimental - */ -export function isFirstRun(): boolean { - let firstRun = false - runOnce(() => { - firstRun = true - }, '__vitest_first_run__') - return firstRun -} - -/** - * @internal - */ -export function resetRunOnceCounter() { - filesCount.clear() -} diff --git a/packages/vitest/src/integrations/utils.ts b/packages/vitest/src/integrations/utils.ts deleted file mode 100644 index 3210c5cbf..000000000 --- a/packages/vitest/src/integrations/utils.ts +++ /dev/null @@ -1,9 +0,0 @@ -// Runtime utils exposed to `vitest` - -export function getRunningMode(): 'watch' | 'run' { - return process.env.VITEST_MODE === 'WATCH' ? 'watch' : 'run' -} - -export function isWatchMode(): boolean { - return getRunningMode() === 'watch' -} diff --git a/packages/vitest/src/node/test-run.ts b/packages/vitest/src/node/test-run.ts index 3760f45ae..f319a5ec9 100644 --- a/packages/vitest/src/node/test-run.ts +++ b/packages/vitest/src/node/test-run.ts @@ -1,5 +1,5 @@ import type { File as RunnerTestFile, TaskEventPack, TaskResultPack, TaskUpdateEvent } from '@vitest/runner' -import type { SerializedError } from '../public/utils' +import type { SerializedError } from '@vitest/utils' import type { UserConsoleLog } from '../types/general' import type { Vitest } from './core' import type { TestProject } from './project' diff --git a/packages/vitest/src/public/browser.ts b/packages/vitest/src/public/browser.ts index f49d115e8..b10a669d5 100644 --- a/packages/vitest/src/public/browser.ts +++ b/packages/vitest/src/public/browser.ts @@ -11,3 +11,14 @@ export { } from '../runtime/setup-common' export { collectTests, processError, startTests } from '@vitest/runner' export * as SpyModule from '@vitest/spy' +export { + format, + getSafeTimers, + inspect, + stringify, +} from '@vitest/utils' +export type { LoupeOptions, ParsedStack, StringifyOptions } from '@vitest/utils' +export { + originalPositionFor, + TraceMap, +} from '@vitest/utils/source-map' diff --git a/packages/vitest/src/public/index.ts b/packages/vitest/src/public/index.ts index db9a995b1..05d2c4b20 100644 --- a/packages/vitest/src/public/index.ts +++ b/packages/vitest/src/public/index.ts @@ -107,9 +107,7 @@ export type { } from '../api/types' export { assert, chai, createExpect, expect, should } from '../integrations/chai' export { inject } from '../integrations/inject' -export { isFirstRun, runOnce } from '../integrations/run-once' -export { getRunningMode, isWatchMode } from '../integrations/utils' export { vi, vitest } from '../integrations/vi' export type { VitestUtils } from '../integrations/vi' export { bench } from '../runtime/benchmark' diff --git a/packages/vitest/src/public/utils.ts b/packages/vitest/src/public/utils.ts deleted file mode 100644 index 29670bc5c..000000000 --- a/packages/vitest/src/public/utils.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from '@vitest/utils' -export * from '@vitest/utils/source-map' diff --git a/packages/vitest/src/runtime/setup-common.ts b/packages/vitest/src/runtime/setup-common.ts index ccea2fbda..7af812f99 100644 --- a/packages/vitest/src/runtime/setup-common.ts +++ b/packages/vitest/src/runtime/setup-common.ts @@ -5,11 +5,9 @@ import type { SerializedConfig } from './config' import type { VitestExecutor } from './execute' import { addSerializer } from '@vitest/snapshot' import { setSafeTimers } from '@vitest/utils' -import { resetRunOnceCounter } from '../integrations/run-once' let globalSetup = false export async function setupCommonEnv(config: SerializedConfig): Promise { - resetRunOnceCounter() setupDefines(config.defines) setupEnv(config.env) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0b3f851bd..1b4dec74a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1312,6 +1312,9 @@ importers: vitest-environment-custom: specifier: file:./vitest-environment-custom version: file:test/core/vitest-environment-custom + vitest-package-exports: + specifier: ^0.1.1 + version: 0.1.1 vue: specifier: 'catalog:' version: 3.5.12(typescript@5.8.3) @@ -9075,6 +9078,9 @@ packages: vitest-environment-custom@file:test/core/vitest-environment-custom: resolution: {directory: test/core/vitest-environment-custom, type: directory} + vitest-package-exports@0.1.1: + resolution: {integrity: sha512-rXgU5noxaaMA5VJXgVPhXRzPiQ1WFmWwgo8dQFPg9j/yFM028scO0IAylFHVI8XMRevhozEt1usa6RQYQY26lg==} + vitest-sonar-reporter@0.5.0: resolution: {integrity: sha512-6BQlx89502l/Z1NVWxN13ir0jCmC8e4/dit5YhoP6U+rX2Cs+CHt3vHnDDSrBwJTVc8QaAaC/hi9XnIH5I28AA==} engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} @@ -17851,6 +17857,11 @@ snapshots: vitest-environment-custom@file:test/core/vitest-environment-custom: {} + vitest-package-exports@0.1.1: + dependencies: + find-up-simple: 1.0.1 + pathe: 2.0.3 + vitest-sonar-reporter@0.5.0(vitest@packages+vitest): dependencies: vitest: link:packages/vitest diff --git a/test/browser/test/utils.test.ts b/test/browser/test/utils.test.ts index f6de6683e..a6920f46f 100644 --- a/test/browser/test/utils.test.ts +++ b/test/browser/test/utils.test.ts @@ -2,7 +2,7 @@ import { commands } from '@vitest/browser/context' import { prettyDOM } from '@vitest/browser/utils' import { afterEach, expect, it, test } from 'vitest' -import { inspect } from 'vitest/utils' +import { inspect } from 'vitest/internal/browser' afterEach(() => { document.body.innerHTML = '' diff --git a/test/core/package.json b/test/core/package.json index df6939379..15e56cf4b 100644 --- a/test/core/package.json +++ b/test/core/package.json @@ -36,6 +36,7 @@ "vite-node": "workspace:*", "vitest": "workspace:*", "vitest-environment-custom": "file:./vitest-environment-custom", + "vitest-package-exports": "^0.1.1", "vue": "catalog:", "zustand": "^4.5.6" } diff --git a/test/core/test/exports.test.ts b/test/core/test/exports.test.ts new file mode 100644 index 000000000..3db0c8c58 --- /dev/null +++ b/test/core/test/exports.test.ts @@ -0,0 +1,166 @@ +import { resolve } from 'node:path' +import { expect, it } from 'vitest' +import { getPackageExportsManifest } from 'vitest-package-exports' + +it('exports snapshot', async ({ skip, task }) => { + skip(task.file.pool !== 'threads', 'run only once inside threads') + + const manifest = await getPackageExportsManifest({ + importMode: 'package', // or 'dist' or 'package' + cwd: resolve(import.meta.dirname, '../../../packages/vitest'), + }) + + expect(manifest.exports) + .toMatchInlineSnapshot(` + { + ".": { + "afterAll": "function", + "afterEach": "function", + "assert": "function", + "assertType": "function", + "beforeAll": "function", + "beforeEach": "function", + "bench": "function", + "chai": "object", + "createExpect": "function", + "describe": "function", + "expect": "function", + "expectTypeOf": "function", + "inject": "function", + "it": "function", + "onTestFailed": "function", + "onTestFinished": "function", + "should": "function", + "suite": "function", + "test": "function", + "vi": "object", + "vitest": "object", + }, + "./config": { + "configDefaults": "object", + "coverageConfigDefaults": "object", + "defaultBrowserPort": "number", + "defaultExclude": "object", + "defaultInclude": "object", + "defineConfig": "function", + "defineProject": "function", + "defineWorkspace": "function", + "extraInlineDeps": "object", + "mergeConfig": "function", + }, + "./coverage": { + "BaseCoverageProvider": "function", + }, + "./environments": { + "builtinEnvironments": "object", + "populateGlobal": "function", + }, + "./execute": { + "VitestExecutor": "function", + }, + "./internal/browser": { + "SpyModule": "object", + "TraceMap": "function", + "collectTests": "function", + "format": "function", + "getSafeTimers": "function", + "inspect": "function", + "loadDiffConfig": "function", + "loadSnapshotSerializers": "function", + "originalPositionFor": "function", + "processError": "function", + "setupCommonEnv": "function", + "startCoverageInsideWorker": "function", + "startTests": "function", + "stopCoverageInsideWorker": "function", + "stringify": "function", + "takeCoverageInsideWorker": "function", + }, + "./mocker": { + "AutomockedModule": "function", + "AutospiedModule": "function", + "ManualMockedModule": "function", + "MockerRegistry": "function", + "RedirectedModule": "function", + "mockObject": "function", + }, + "./node": { + "BaseSequencer": "function", + "GitNotFoundError": "function", + "TestFile": "function", + "TestsNotFoundError": "function", + "VitestPackageInstaller": "function", + "VitestPlugin": "function", + "createDebugger": "function", + "createMethodsRPC": "function", + "createServer": "function", + "createViteLogger": "function", + "createViteServer": "function", + "createVitest": "function", + "distDir": "string", + "esbuildVersion": "string", + "generateFileHash": "function", + "getFilePoolName": "function", + "isFileServingAllowed": "function", + "isValidApiRequest": "function", + "parseAst": "function", + "parseAstAsync": "function", + "parseCLI": "function", + "registerConsoleShortcuts": "function", + "resolveApiServerConfig": "function", + "resolveConfig": "function", + "resolveFsAllow": "function", + "rollupVersion": "string", + "rootDir": "string", + "startVitest": "function", + "version": "string", + "viteVersion": "string", + }, + "./reporters": { + "BasicReporter": "function", + "BenchmarkReporter": "function", + "BenchmarkReportsMap": "object", + "DefaultReporter": "function", + "DotReporter": "function", + "GithubActionsReporter": "function", + "HangingProcessReporter": "function", + "JUnitReporter": "function", + "JsonReporter": "function", + "ReportersMap": "object", + "TapFlatReporter": "function", + "TapReporter": "function", + "VerboseBenchmarkReporter": "function", + "VerboseReporter": "function", + }, + "./runners": { + "NodeBenchmarkRunner": "function", + "VitestTestRunner": "function", + }, + "./snapshot": { + "VitestSnapshotEnvironment": "function", + }, + "./suite": { + "createChainable": "function", + "createTaskCollector": "function", + "getBenchFn": "function", + "getBenchOptions": "function", + "getCurrentSuite": "function", + "getCurrentTest": "function", + "getFn": "function", + "getHooks": "function", + "setFn": "function", + "setHooks": "function", + }, + "./workers": { + "collectVitestWorkerTests": "function", + "createForksRpcOptions": "function", + "createThreadsRpcOptions": "function", + "provideWorkerState": "function", + "runBaseTests": "function", + "runVitestWorker": "function", + "runVmTests": "function", + "unwrapSerializableConfig": "function", + }, + } + `) +}) diff --git a/tsconfig.base.json b/tsconfig.base.json index 7969262c3..a13f0f293 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -29,7 +29,7 @@ "vitest/execute": ["./packages/vitest/src/public/execute.ts"], "vitest/config": ["./packages/vitest/src/public/config.ts"], "vitest/coverage": ["./packages/vitest/src/public/coverage.ts"], - "vitest/browser": ["./packages/vitest/src/public/browser.ts"], + "vitest/internal/browser": ["./packages/vitest/src/public/browser.ts"], "vitest/runners": ["./packages/vitest/src/public/runners.ts"], "vite-node": ["./packages/vite-node/src/index.ts"], "vite-node/client": ["./packages/vite-node/src/client.ts"],