Skip to content

Commit

Permalink
fix(vitest): correctly send console events when state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 28, 2024
1 parent 7e91781 commit 3463f9b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
16 changes: 10 additions & 6 deletions packages/vitest/src/runtime/console.ts
Expand Up @@ -3,7 +3,7 @@ import { Console } from 'node:console'
import { relative } from 'node:path'
import { getColors, getSafeTimers } from '@vitest/utils'
import { RealDate } from '../integrations/mock/date'
import type { WorkerGlobalState } from '../types'
import { getWorkerState } from '../utils'

export const UNKNOWN_TEST_ID = '__vitest__unknown_test__'

Expand All @@ -27,13 +27,15 @@ function getTaskIdByStack(root: string) {
return UNKNOWN_TEST_ID
}

export function createCustomConsole(state: WorkerGlobalState) {
export function createCustomConsole() {
const stdoutBuffer = new Map<string, any[]>()
const stderrBuffer = new Map<string, any[]>()
const timers = new Map<string, { stdoutTime: number; stderrTime: number; timer: any }>()

const { setTimeout, clearTimeout } = getSafeTimers()

const state = () => getWorkerState()

// group sync console.log calls with macro task
function schedule(taskId: string) {
const timer = timers.get(taskId)!
Expand All @@ -56,7 +58,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
return
const content = buffer.map(i => String(i)).join('')
const timer = timers.get(taskId)!
state.rpc.onUserConsoleLog({
state().rpc.onUserConsoleLog({
type: 'stdout',
content: content || '<empty line>',
taskId,
Expand All @@ -72,7 +74,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
return
const content = buffer.map(i => String(i)).join('')
const timer = timers.get(taskId)!
state.rpc.onUserConsoleLog({
state().rpc.onUserConsoleLog({
type: 'stderr',
content: content || '<empty line>',
taskId,
Expand All @@ -85,7 +87,8 @@ export function createCustomConsole(state: WorkerGlobalState) {

const stdout = new Writable({
write(data, encoding, callback) {
const id = state?.current?.id || state?.current?.file?.id || getTaskIdByStack(state.ctx.config.root)
const s = state()
const id = s?.current?.id || s?.current?.file?.id || getTaskIdByStack(s.config.root)
let timer = timers.get(id)
if (timer) {
timer.stdoutTime = timer.stdoutTime || RealDate.now()
Expand All @@ -106,7 +109,8 @@ export function createCustomConsole(state: WorkerGlobalState) {
})
const stderr = new Writable({
write(data, encoding, callback) {
const id = state?.current?.id || state?.current?.file?.id || getTaskIdByStack(state.ctx.config.root)
const s = state()
const id = s?.current?.id || s?.current?.file?.id || getTaskIdByStack(s.config.root)
let timer = timers.get(id)
if (timer) {
timer.stderrTime = timer.stderrTime || RealDate.now()
Expand Down
8 changes: 4 additions & 4 deletions packages/vitest/src/runtime/setup-node.ts
Expand Up @@ -4,7 +4,7 @@ import timers from 'node:timers'
import { isatty } from 'node:tty'
import { installSourcemapsSupport } from 'vite-node/source-map'
import { createColors, setupColors } from '@vitest/utils'
import type { EnvironmentOptions, ResolvedConfig, ResolvedTestEnvironment, WorkerGlobalState } from '../types'
import type { EnvironmentOptions, ResolvedConfig, ResolvedTestEnvironment } from '../types'
import { VitestSnapshotEnvironment } from '../integrations/snapshot/environments/node'
import { getSafeTimers, getWorkerState } from '../utils'
import * as VitestIndex from '../index'
Expand Down Expand Up @@ -56,13 +56,13 @@ export async function setupGlobalEnv(config: ResolvedConfig, { environment }: Re
})

if (!config.disableConsoleIntercept)
await setupConsoleLogSpy(state)
await setupConsoleLogSpy()
}

export async function setupConsoleLogSpy(state: WorkerGlobalState) {
export async function setupConsoleLogSpy() {
const { createCustomConsole } = await import('./console')

globalThis.console = createCustomConsole(state)
globalThis.console = createCustomConsole()
}

export async function withEnv(
Expand Down

0 comments on commit 3463f9b

Please sign in to comment.