Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function createCustomConsole(defaultState?: WorkerGlobalState) {
sendLog(type, taskId, content, buffer.length)
}
const timer = timers.get(taskId)!
buffers.set(taskId, [])
buffers.delete(taskId)
if (type === 'stderr') {
timer.stderrTime = 0
}
Expand Down
9 changes: 9 additions & 0 deletions test/reporters/fixtures/console-interleave.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from 'vitest';

test('repro', async () => {
console.log(1);
await new Promise((r) => setTimeout(r, 10));
console.error(2);
await new Promise((r) => setTimeout(r, 10));
console.log(3);
});
33 changes: 32 additions & 1 deletion test/reporters/tests/console.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'pathe'
import { expect, test } from 'vitest'
import { type UserConsoleLog, expect, test } from 'vitest'
import type { Reporter } from 'vitest/reporters'
import { DefaultReporter } from 'vitest/reporters'
import { runVitest } from '../../test-utils'

Expand Down Expand Up @@ -66,3 +67,33 @@ stderr | console.test.ts
global stderr afterAll`,
)
})

test.for(['forks', 'threads'])('interleave (pool = %s)', async (pool) => {
const logs: UserConsoleLog[] = []
const { stderr } = await runVitest({
root: './fixtures',
pool,
reporters: [
{
onUserConsoleLog(log) {
logs.push(log)
},
} satisfies Reporter,
],
}, [resolve('./fixtures/console-interleave.test.ts')])
expect(stderr).toBe('')
expect(logs).toMatchObject([
{
type: 'stdout',
content: expect.stringContaining('1'),
},
{
type: 'stderr',
content: expect.stringContaining('2'),
},
{
type: 'stdout',
content: expect.stringContaining('3'),
},
])
})