Skip to content

Commit a60b8e1

Browse files
authored
fix: don't fail on console log pending at teardown (#11619)
1 parent 1453ba6 commit a60b8e1

2 files changed

Lines changed: 59 additions & 10 deletions

File tree

‎packages/vitest/src/runtime/console.ts‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { relative } from 'node:path'
44
import { Writable } from 'node:stream'
55
import { getSafeTimers } from '@vitest/utils/timers'
66
import c from 'tinyrainbow'
7-
import { getWorkerState } from './utils'
7+
import { EnvironmentTeardownError, getWorkerState } from './utils'
88

99
const RealDate = globalThis.Date
1010

@@ -109,14 +109,21 @@ export function createCustomConsole(defaultState?: WorkerGlobalState): Console {
109109
) {
110110
const timer = timers.get(taskId)!
111111
const time = type === 'stderr' ? timer.stderrTime : timer.stdoutTime
112-
state().rpc.onUserConsoleLog({
113-
type,
114-
content: content || '<empty line>',
115-
taskId,
116-
time: time || RealDate.now(),
117-
size,
118-
origin,
119-
})
112+
state()
113+
.rpc.onUserConsoleLog({
114+
type,
115+
content: content || '<empty line>',
116+
taskId,
117+
time: time || RealDate.now(),
118+
size,
119+
origin,
120+
})
121+
.catch((error) => {
122+
// the log is already sent, the worker only closed before the reporter answered
123+
if (!(error instanceof EnvironmentTeardownError)) {
124+
throw error
125+
}
126+
})
120127
}
121128

122129
const stdout = new Writable({

‎test/e2e/test/console.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { relative, resolve } from 'pathe'
22
import { expect, test } from 'vitest'
33
import { DefaultReporter } from 'vitest/node'
4-
import { runInlineTests, runVitest } from '../../test-utils'
4+
import { runInlineTests, runVitest, ts } from '../../test-utils'
55

66
test('can run custom pools with Vitest', async () => {
77
const reporter = new DefaultReporter()
@@ -152,3 +152,45 @@ test('onConsoleLog receives the entity', async () => {
152152
]
153153
`)
154154
})
155+
156+
// https://github.com/vitest-dev/vitest/issues/11153
157+
test('console log sent while the worker closes is not reported as an error', async () => {
158+
const { stderr, testTree } = await runInlineTests(
159+
{
160+
'late-log.test.ts': ts`
161+
import { test } from 'vitest'
162+
163+
test('logs and leaks an interval', () => {
164+
console.log('log inside the test')
165+
let ticks = 0
166+
const interval = setInterval(() => {
167+
console.log('log after the test')
168+
if (++ticks === 50) {
169+
clearInterval(interval)
170+
}
171+
}, 2)
172+
})
173+
`,
174+
},
175+
{
176+
reporters: [
177+
'verbose',
178+
{
179+
async onUserConsoleLog() {
180+
// keep every log pending, so the worker closes while the late logs wait for an answer
181+
await new Promise((resolve) => setTimeout(resolve, 20))
182+
},
183+
},
184+
],
185+
},
186+
)
187+
188+
expect(stderr).toBe('')
189+
expect(testTree()).toMatchInlineSnapshot(`
190+
{
191+
"late-log.test.ts": {
192+
"logs and leaks an interval": "passed",
193+
},
194+
}
195+
`)
196+
})

0 commit comments

Comments
 (0)