Skip to content

Commit 418a3b5

Browse files
committed
fix: print stdout/stderr logs, not just console.*
Fixes #585
1 parent 5862ff1 commit 418a3b5

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

src/api.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ export class VitestFolderAPI {
228228
this.handlers.clearListeners()
229229
}
230230

231-
private createHandler<K extends Exclude<keyof ResolvedMeta['handlers'], 'clearListeners' | 'removeListener'>>(name: K) {
231+
onStdout(callback: (log: string) => void) {
232+
this.handlers.onStdout(callback)
233+
}
234+
235+
private createHandler<K extends Exclude<keyof ResolvedMeta['handlers'], 'clearListeners' | 'removeListener' | 'onStdout'>>(name: K) {
232236
return (callback: VitestEvents[K]) => {
233237
this.handlers[name](callback as any)
234238
}
@@ -345,6 +349,7 @@ export interface ResolvedMeta {
345349
pkg: VitestPackage
346350
configs: string[]
347351
handlers: {
352+
onStdout: (listener: (log: string) => void) => void
348353
onConsoleLog: (listener: VitestEvents['onConsoleLog']) => void
349354
onTaskUpdate: (listener: VitestEvents['onTaskUpdate']) => void
350355
onFinished: (listener: VitestEvents['onFinished']) => void

src/api/child_process.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,27 @@ export async function createVitestProcess(pkg: VitestPackage) {
5151
cwd: pkg.cwd,
5252
})
5353

54-
vitest.stdout?.on('data', d => log.worker('info', d.toString()))
54+
const stdoutCallbacks = new Set<(data: string) => void>()
55+
56+
vitest.stdout?.on('data', (d) => {
57+
const content = d.toString()
58+
stdoutCallbacks.forEach(cb => cb(content))
59+
log.worker('info', content)
60+
})
5561
vitest.stderr?.on('data', (chunk) => {
5662
const string = chunk.toString()
5763
log.worker('error', string)
64+
stdoutCallbacks.forEach(cb => cb(string))
5865
if (string.startsWith(' MISSING DEPENDENCY')) {
5966
const error = string.split(/\r?\n/, 1)[0].slice(' MISSING DEPENDENCY'.length)
6067
showVitestError(error)
6168
}
6269
})
6370

71+
vitest.on('exit', () => {
72+
stdoutCallbacks.clear()
73+
})
74+
6475
return new Promise<ResolvedMeta>((resolve, reject) => {
6576
function onExit(code: number | null) {
6677
reject(new Error(`Vitest process exited with code ${code}`))
@@ -69,7 +80,17 @@ export async function createVitestProcess(pkg: VitestPackage) {
6980
vitest.on('exit', onExit)
7081

7182
waitForWsResolvedMeta(wss, pkg, false, 'child_process', vitest)
72-
.then(resolve, reject)
83+
.then((resolved) => {
84+
resolved.handlers.onStdout = (callback: (data: string) => void) => {
85+
stdoutCallbacks.add(callback)
86+
}
87+
const clearListeners = resolved.handlers.clearListeners
88+
resolved.handlers.clearListeners = () => {
89+
clearListeners()
90+
stdoutCallbacks.clear()
91+
}
92+
resolve(resolved)
93+
}, reject)
7394
.finally(() => {
7495
vitest.off('exit', onExit)
7596
})

src/api/ws.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ export function waitForWsResolvedMeta(
3636
})
3737
resolve({
3838
rpc: api,
39-
handlers,
39+
handlers: {
40+
...handlers,
41+
onStdout() {
42+
// do nothing by default
43+
},
44+
},
4045
configs: message.configs,
4146
process: new VitestWebSocketProcess(Math.random(), wss, ws, child),
4247
pkg,

src/log.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,21 @@ import { getConfig } from './config'
66

77
const logFile = process.env.VITEST_VSCODE_E2E_LOG_FILE!
88
const channel = window.createOutputChannel('Vitest')
9+
const callbacks: Set<((message: string) => void)> = new Set()
10+
11+
function logToCallbacks(message: string) {
12+
for (const callback of callbacks) {
13+
callback(message)
14+
}
15+
}
16+
917
export const log = {
18+
onWorkerLog(callback: (message: string) => void) {
19+
callbacks.add(callback)
20+
},
21+
offWorkerLog(callback: (message: string) => void) {
22+
callbacks.delete(callback)
23+
},
1024
worker: (type: 'info' | 'error', ...args: any[]) => {
1125
if (typeof args.at(-1) === 'string' && args.at(-1).endsWith('\n'))
1226
args[args.length - 1] = args.at(-1).slice(0, process.platform === 'win32' ? -2 : -1)
@@ -19,6 +33,7 @@ export const log = {
1933
if (logFile) {
2034
appendFile(message)
2135
}
36+
logToCallbacks(message)
2237
channel.appendLine(message)
2338
},
2439
info: (...args: any[]) => {

src/runner/runner.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ export class TestRunner extends vscode.Disposable {
4343
this.disposables = []
4444
})
4545

46+
api.onStdout((content) => {
47+
if (this.testRun) {
48+
this.testRun.appendOutput(formatTestOutput(content))
49+
}
50+
})
51+
4652
api.onWatcherRerun((files, _trigger, collecting) => {
4753
if (collecting) {
4854
log.verbose?.('Not starting the runner because tests are being collected for', ...files.map(f => this.relative(f)))

0 commit comments

Comments
 (0)