diff --git a/packages/vitest/src/node/error.ts b/packages/vitest/src/node/error.ts index a3f12ef63a76..683d0c314568 100644 --- a/packages/vitest/src/node/error.ts +++ b/packages/vitest/src/node/error.ts @@ -1,7 +1,7 @@ /* eslint-disable prefer-template */ /* eslint-disable no-template-curly-in-string */ import { existsSync, readFileSync } from 'fs' -import { join, relative } from 'pathe' +import { join, normalize, relative } from 'pathe' import c from 'picocolors' import cliTruncate from 'cli-truncate' import type { ErrorWithDiff, ParsedStack, Position } from '../types' @@ -108,7 +108,7 @@ function handleImportOutsideModuleError(stack: string, ctx: Vitest) { if (!esmErrors.some(e => stack.includes(e))) return - const path = stack.split('\n')[0].trim() + const path = normalize(stack.split('\n')[0].trim()) let name = path.split('/node_modules/').pop() || '' if (name?.startsWith('@')) name = name.split('/').slice(0, 2).join('/') diff --git a/packages/vitest/src/node/stdin.ts b/packages/vitest/src/node/stdin.ts index 1595c331f578..847769ba4219 100644 --- a/packages/vitest/src/node/stdin.ts +++ b/packages/vitest/src/node/stdin.ts @@ -1,6 +1,7 @@ import readline from 'readline' import c from 'picocolors' import prompt from 'prompts' +import { stdout } from '../utils' import type { Vitest } from './core' const keys = [ @@ -12,7 +13,7 @@ const keys = [ ] export function printShortcutsHelp() { - process.stdout.write( + stdout().write( ` ${c.bold(' Watch Usage')} ${keys.map(i => c.dim(' press ') + c.reset(c.bold(i[0])) + c.dim(` to ${i[1]}`)).join('\n')} diff --git a/packages/vitest/src/runtime/collect.ts b/packages/vitest/src/runtime/collect.ts index fcb210803512..35a74eb583bb 100644 --- a/packages/vitest/src/runtime/collect.ts +++ b/packages/vitest/src/runtime/collect.ts @@ -1,6 +1,7 @@ import { createHash } from 'crypto' import { relative } from 'pathe' import type { File, ResolvedConfig, Suite, TaskBase } from '../types' +import { stdout } from '../utils' import { clearCollectorContext, defaultSuite } from './suite' import { getHooks, setHooks } from './map' import { processError } from './error' @@ -61,7 +62,7 @@ export async function collectTests(paths: string[], config: ResolvedConfig) { error: processError(e), } // not sure thy, this line is needed to trigger the error - process.stdout.write('\0') + stdout().write('\0') } calculateHash(file) diff --git a/packages/vitest/src/runtime/worker.ts b/packages/vitest/src/runtime/worker.ts index fe827f25c852..f0d3d2c5aed4 100644 --- a/packages/vitest/src/runtime/worker.ts +++ b/packages/vitest/src/runtime/worker.ts @@ -3,7 +3,7 @@ import { createBirpc } from 'birpc' import { ModuleCacheMap } from 'vite-node/client' import type { ResolvedConfig, WorkerContext, WorkerRPC } from '../types' import { distDir } from '../constants' -import { getWorkerState } from '../utils' +import { getWorkerState, stdout } from '../utils' import type { MockMap } from '../types/mocker' import { executeInViteNode } from './execute' import { rpc } from './rpc' @@ -64,7 +64,7 @@ function init(ctx: WorkerContext) { if (typeof __vitest_worker__ !== 'undefined' && ctx.config.threads && ctx.config.isolate) throw new Error(`worker for ${ctx.files.join(',')} already initialized by ${getWorkerState().ctx.files.join(',')}. This is probably an internal bug of Vitest.`) - process.stdout.write('\0') + stdout().write('\0') const { config, port, id } = ctx diff --git a/packages/vitest/src/utils/base.ts b/packages/vitest/src/utils/base.ts index c231f1b69431..3874d9684815 100644 --- a/packages/vitest/src/utils/base.ts +++ b/packages/vitest/src/utils/base.ts @@ -126,3 +126,9 @@ export function assertTypes(value: unknown, name: string, types: string[]): void if (!pass) throw new TypeError(`${name} value must be ${types.join(' or ')}, received "${receivedType}"`) } + +export function stdout(): NodeJS.WriteStream { + // @ts-expect-error Node.js maps process.stdout to console._stdout + // eslint-disable-next-line no-console + return console._stdout || process.stdout +}