Skip to content

Commit

Permalink
fix: use console._stdout, if available (#1235)
Browse files Browse the repository at this point in the history
* fix: use console._stdout, if available

* chore: normalize module path in outside of module error helper

So it will look right on windows
  • Loading branch information
sheremet-va committed May 5, 2022
1 parent 93422cb commit d15d47f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions 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'
Expand Down Expand Up @@ -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('/')
Expand Down
3 changes: 2 additions & 1 deletion 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 = [
Expand All @@ -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')}
Expand Down
3 changes: 2 additions & 1 deletion 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'
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/worker.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/utils/base.ts
Expand Up @@ -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
}

0 comments on commit d15d47f

Please sign in to comment.