Skip to content

Commit ce5ca6b

Browse files
authoredMar 15, 2024··
fix(vitest): logs in beforeAll and afterAll (#5288)
1 parent 51d1d47 commit ce5ca6b

File tree

7 files changed

+137
-7
lines changed

7 files changed

+137
-7
lines changed
 

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
8585

8686
const stdout = new Writable({
8787
write(data, encoding, callback) {
88-
const id = state?.current?.id ?? getTaskIdByStack(state.ctx.config.root)
88+
const id = state?.current?.id || state?.current?.file?.id || getTaskIdByStack(state.ctx.config.root)
8989
let timer = timers.get(id)
9090
if (timer) {
9191
timer.stdoutTime = timer.stdoutTime || RealDate.now()
@@ -106,7 +106,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
106106
})
107107
const stderr = new Writable({
108108
write(data, encoding, callback) {
109-
const id = state?.current?.id ?? getTaskIdByStack(state.ctx.config.root)
109+
const id = state?.current?.id || state?.current?.file?.id || getTaskIdByStack(state.ctx.config.root)
110110
let timer = timers.get(id)
111111
if (timer) {
112112
timer.stderrTime = timer.stderrTime || RealDate.now()

‎packages/vitest/src/runtime/runners/test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export class VitestTestRunner implements VitestRunner {
2727
this.snapshotClient.clear()
2828
}
2929

30+
onAfterRunFiles() {
31+
this.workerState.current = undefined
32+
}
33+
3034
async onAfterRunSuite(suite: Suite) {
3135
if (this.config.logHeapUsage && typeof process !== 'undefined')
3236
suite.result!.heap = process.memoryUsage().heapUsed
@@ -44,6 +48,8 @@ export class VitestTestRunner implements VitestRunner {
4448
if (result)
4549
await rpc().snapshotSaved(result)
4650
}
51+
52+
this.workerState.current = suite.suite
4753
}
4854

4955
onAfterRunTask(test: Task) {
@@ -52,7 +58,7 @@ export class VitestTestRunner implements VitestRunner {
5258
if (this.config.logHeapUsage && typeof process !== 'undefined')
5359
test.result!.heap = process.memoryUsage().heapUsed
5460

55-
this.workerState.current = undefined
61+
this.workerState.current = test.suite
5662
}
5763

5864
onCancel(_reason: CancelReason) {
@@ -81,6 +87,8 @@ export class VitestTestRunner implements VitestRunner {
8187
// (e.g. `toMatchSnapshot`) specifies "filepath" / "name" pair explicitly
8288
await this.snapshotClient.startCurrentRun(suite.filepath, '__default_name_', this.workerState.config.snapshotOptions)
8389
}
90+
91+
this.workerState.current = suite
8492
}
8593

8694
onBeforeTryTask(test: Task) {
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
2+
3+
beforeAll(() => {
4+
console.log('beforeAll')
5+
console.error('beforeAll')
6+
})
7+
8+
afterAll(() => {
9+
console.log('afterAll')
10+
console.error('afterAll')
11+
})
12+
13+
describe('suite', () => {
14+
beforeAll(() => {
15+
console.log('beforeAll')
16+
console.error('beforeAll')
17+
})
18+
19+
afterAll(() => {
20+
console.log('afterAll')
21+
console.error('afterAll')
22+
})
23+
24+
describe('nested suite', () => {
25+
beforeAll(() => {
26+
console.log('beforeAll')
27+
console.error('beforeAll')
28+
})
29+
30+
afterAll(() => {
31+
console.log('afterAll')
32+
console.error('afterAll')
33+
})
34+
35+
test('test', () => {
36+
expect(true).toBe(true)
37+
})
38+
})
39+
})

‎test/reporters/tests/console.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { resolve } from 'pathe'
2+
import { expect, test } from 'vitest'
3+
import { runVitest } from '../../test-utils'
4+
5+
test('should print logs correctly', async () => {
6+
const filename = resolve('./fixtures/console.test.ts')
7+
const { stdout, stderr } = await runVitest({ root: './fixtures' }, [filename])
8+
9+
expect(stdout).toBeTruthy()
10+
expect(stderr).toBeTruthy()
11+
12+
expect(stdout).toContain(
13+
`
14+
stdout | console.test.ts > suite > nested suite
15+
beforeAll
16+
afterAll
17+
18+
stdout | console.test.ts > suite
19+
beforeAll
20+
afterAll
21+
22+
stdout | console.test.ts
23+
beforeAll
24+
afterAll
25+
`,
26+
)
27+
28+
expect(stderr).toContain(
29+
`stderr | console.test.ts > suite > nested suite
30+
beforeAll
31+
afterAll
32+
33+
stderr | console.test.ts > suite
34+
beforeAll
35+
afterAll
36+
37+
stderr | console.test.ts
38+
beforeAll
39+
afterAll`,
40+
)
41+
})

‎test/ui/fixtures/console.test.ts

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-console */
22

3-
import { it } from "vitest";
4-
import { prettyDOM } from "@testing-library/dom"
3+
import { afterAll, beforeAll, it, describe, expect } from "vitest";
4+
import { prettyDOM } from "@testing-library/dom";
55

66
// https://github.com/vitest-dev/vitest/issues/2765
77
it('regexp', () => {
@@ -31,3 +31,42 @@ it('html-pretty', () => {
3131
`.replaceAll(/\n */gm, ""); // strip new liens
3232
console.log(prettyDOM(div))
3333
})
34+
35+
36+
beforeAll(() => {
37+
console.log('beforeAll')
38+
console.error('beforeAll')
39+
})
40+
41+
afterAll(() => {
42+
console.log('afterAll')
43+
console.error('afterAll')
44+
})
45+
46+
describe('suite', () => {
47+
beforeAll(() => {
48+
console.log('beforeAll')
49+
console.error('beforeAll')
50+
})
51+
52+
afterAll(() => {
53+
console.log('afterAll')
54+
console.error('afterAll')
55+
})
56+
57+
describe('nested suite', () => {
58+
beforeAll(() => {
59+
console.log('beforeAll')
60+
console.error('beforeAll')
61+
})
62+
63+
afterAll(() => {
64+
console.log('afterAll')
65+
console.error('afterAll')
66+
})
67+
68+
it('test', () => {
69+
expect(true).toBe(true)
70+
})
71+
})
72+
})

‎test/ui/test/html-report.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test.describe('html report', () => {
3232
await page.goto(pageUrl)
3333

3434
// dashbaord
35-
await expect(page.locator('[aria-labelledby=tests]')).toContainText('5 Pass 1 Fail 6 Total')
35+
await expect(page.locator('[aria-labelledby=tests]')).toContainText('6 Pass 1 Fail 7 Total')
3636

3737
// unhandled errors
3838
await expect(page.getByTestId('unhandled-errors')).toContainText(

‎test/ui/test/ui.spec.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test.describe('ui', () => {
2323
await page.goto(pageUrl)
2424

2525
// dashbaord
26-
await expect(page.locator('[aria-labelledby=tests]')).toContainText('5 Pass 1 Fail 6 Total')
26+
await expect(page.locator('[aria-labelledby=tests]')).toContainText('6 Pass 1 Fail 7 Total')
2727

2828
// unhandled errors
2929
await expect(page.getByTestId('unhandled-errors')).toContainText(
@@ -61,6 +61,9 @@ test.describe('ui', () => {
6161
await page.getByText('fixtures/console.test.ts').click()
6262
await page.getByTestId('btn-console').click()
6363
await page.getByText('/(?<char>\\w)/').click()
64+
65+
expect(await page.getByText('beforeAll').all()).toHaveLength(6)
66+
expect(await page.getByText('afterAll').all()).toHaveLength(6)
6467
})
6568

6669
test('error', async ({ page }) => {

0 commit comments

Comments
 (0)
Please sign in to comment.