Skip to content

Commit

Permalink
fix(vitest): fix <empty line> logs when interleaving `console.log/e…
Browse files Browse the repository at this point in the history
…rror` (#6644)
  • Loading branch information
hi-ogawa authored Oct 6, 2024
1 parent ba97f6a commit 9ece395
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
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'),
},
])
})

0 comments on commit 9ece395

Please sign in to comment.