From ce5ca6bf6c8f9b30a2331b2dcd761e72c13392e9 Mon Sep 17 00:00:00 2001 From: Han Date: Fri, 15 Mar 2024 16:59:14 +0800 Subject: [PATCH] fix(vitest): logs in `beforeAll` and `afterAll` (#5288) --- packages/vitest/src/runtime/console.ts | 4 +- packages/vitest/src/runtime/runners/test.ts | 10 ++++- test/reporters/fixtures/console.test.ts | 39 +++++++++++++++++++ test/reporters/tests/console.test.ts | 41 ++++++++++++++++++++ test/ui/fixtures/console.test.ts | 43 ++++++++++++++++++++- test/ui/test/html-report.spec.ts | 2 +- test/ui/test/ui.spec.ts | 5 ++- 7 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 test/reporters/fixtures/console.test.ts create mode 100644 test/reporters/tests/console.test.ts diff --git a/packages/vitest/src/runtime/console.ts b/packages/vitest/src/runtime/console.ts index 45f8a7b20718..14ea15532325 100644 --- a/packages/vitest/src/runtime/console.ts +++ b/packages/vitest/src/runtime/console.ts @@ -85,7 +85,7 @@ export function createCustomConsole(state: WorkerGlobalState) { const stdout = new Writable({ write(data, encoding, callback) { - const id = state?.current?.id ?? getTaskIdByStack(state.ctx.config.root) + const id = state?.current?.id || state?.current?.file?.id || getTaskIdByStack(state.ctx.config.root) let timer = timers.get(id) if (timer) { timer.stdoutTime = timer.stdoutTime || RealDate.now() @@ -106,7 +106,7 @@ export function createCustomConsole(state: WorkerGlobalState) { }) const stderr = new Writable({ write(data, encoding, callback) { - const id = state?.current?.id ?? getTaskIdByStack(state.ctx.config.root) + const id = state?.current?.id || state?.current?.file?.id || getTaskIdByStack(state.ctx.config.root) let timer = timers.get(id) if (timer) { timer.stderrTime = timer.stderrTime || RealDate.now() diff --git a/packages/vitest/src/runtime/runners/test.ts b/packages/vitest/src/runtime/runners/test.ts index c79231941708..e567506e75fc 100644 --- a/packages/vitest/src/runtime/runners/test.ts +++ b/packages/vitest/src/runtime/runners/test.ts @@ -27,6 +27,10 @@ export class VitestTestRunner implements VitestRunner { this.snapshotClient.clear() } + onAfterRunFiles() { + this.workerState.current = undefined + } + async onAfterRunSuite(suite: Suite) { if (this.config.logHeapUsage && typeof process !== 'undefined') suite.result!.heap = process.memoryUsage().heapUsed @@ -44,6 +48,8 @@ export class VitestTestRunner implements VitestRunner { if (result) await rpc().snapshotSaved(result) } + + this.workerState.current = suite.suite } onAfterRunTask(test: Task) { @@ -52,7 +58,7 @@ export class VitestTestRunner implements VitestRunner { if (this.config.logHeapUsage && typeof process !== 'undefined') test.result!.heap = process.memoryUsage().heapUsed - this.workerState.current = undefined + this.workerState.current = test.suite } onCancel(_reason: CancelReason) { @@ -81,6 +87,8 @@ export class VitestTestRunner implements VitestRunner { // (e.g. `toMatchSnapshot`) specifies "filepath" / "name" pair explicitly await this.snapshotClient.startCurrentRun(suite.filepath, '__default_name_', this.workerState.config.snapshotOptions) } + + this.workerState.current = suite } onBeforeTryTask(test: Task) { diff --git a/test/reporters/fixtures/console.test.ts b/test/reporters/fixtures/console.test.ts new file mode 100644 index 000000000000..dde0264286f0 --- /dev/null +++ b/test/reporters/fixtures/console.test.ts @@ -0,0 +1,39 @@ +import { afterAll, beforeAll, describe, expect, test } from 'vitest' + +beforeAll(() => { + console.log('beforeAll') + console.error('beforeAll') +}) + +afterAll(() => { + console.log('afterAll') + console.error('afterAll') +}) + +describe('suite', () => { + beforeAll(() => { + console.log('beforeAll') + console.error('beforeAll') + }) + + afterAll(() => { + console.log('afterAll') + console.error('afterAll') + }) + + describe('nested suite', () => { + beforeAll(() => { + console.log('beforeAll') + console.error('beforeAll') + }) + + afterAll(() => { + console.log('afterAll') + console.error('afterAll') + }) + + test('test', () => { + expect(true).toBe(true) + }) + }) +}) diff --git a/test/reporters/tests/console.test.ts b/test/reporters/tests/console.test.ts new file mode 100644 index 000000000000..b84f6e8f968f --- /dev/null +++ b/test/reporters/tests/console.test.ts @@ -0,0 +1,41 @@ +import { resolve } from 'pathe' +import { expect, test } from 'vitest' +import { runVitest } from '../../test-utils' + +test('should print logs correctly', async () => { + const filename = resolve('./fixtures/console.test.ts') + const { stdout, stderr } = await runVitest({ root: './fixtures' }, [filename]) + + expect(stdout).toBeTruthy() + expect(stderr).toBeTruthy() + + expect(stdout).toContain( +` +stdout | console.test.ts > suite > nested suite +beforeAll +afterAll + +stdout | console.test.ts > suite +beforeAll +afterAll + +stdout | console.test.ts +beforeAll +afterAll +`, + ) + + expect(stderr).toContain( +`stderr | console.test.ts > suite > nested suite +beforeAll +afterAll + +stderr | console.test.ts > suite +beforeAll +afterAll + +stderr | console.test.ts +beforeAll +afterAll`, + ) +}) diff --git a/test/ui/fixtures/console.test.ts b/test/ui/fixtures/console.test.ts index 2550b9a32503..8318c03bd7aa 100644 --- a/test/ui/fixtures/console.test.ts +++ b/test/ui/fixtures/console.test.ts @@ -1,7 +1,7 @@ /* eslint-disable no-console */ -import { it } from "vitest"; -import { prettyDOM } from "@testing-library/dom" +import { afterAll, beforeAll, it, describe, expect } from "vitest"; +import { prettyDOM } from "@testing-library/dom"; // https://github.com/vitest-dev/vitest/issues/2765 it('regexp', () => { @@ -31,3 +31,42 @@ it('html-pretty', () => { `.replaceAll(/\n */gm, ""); // strip new liens console.log(prettyDOM(div)) }) + + +beforeAll(() => { + console.log('beforeAll') + console.error('beforeAll') +}) + +afterAll(() => { + console.log('afterAll') + console.error('afterAll') +}) + +describe('suite', () => { + beforeAll(() => { + console.log('beforeAll') + console.error('beforeAll') + }) + + afterAll(() => { + console.log('afterAll') + console.error('afterAll') + }) + + describe('nested suite', () => { + beforeAll(() => { + console.log('beforeAll') + console.error('beforeAll') + }) + + afterAll(() => { + console.log('afterAll') + console.error('afterAll') + }) + + it('test', () => { + expect(true).toBe(true) + }) + }) +}) diff --git a/test/ui/test/html-report.spec.ts b/test/ui/test/html-report.spec.ts index 0fd7530b024d..b7b732460684 100644 --- a/test/ui/test/html-report.spec.ts +++ b/test/ui/test/html-report.spec.ts @@ -32,7 +32,7 @@ test.describe('html report', () => { await page.goto(pageUrl) // dashbaord - await expect(page.locator('[aria-labelledby=tests]')).toContainText('5 Pass 1 Fail 6 Total') + await expect(page.locator('[aria-labelledby=tests]')).toContainText('6 Pass 1 Fail 7 Total') // unhandled errors await expect(page.getByTestId('unhandled-errors')).toContainText( diff --git a/test/ui/test/ui.spec.ts b/test/ui/test/ui.spec.ts index a54e1a2c608d..e2703dffed0c 100644 --- a/test/ui/test/ui.spec.ts +++ b/test/ui/test/ui.spec.ts @@ -23,7 +23,7 @@ test.describe('ui', () => { await page.goto(pageUrl) // dashbaord - await expect(page.locator('[aria-labelledby=tests]')).toContainText('5 Pass 1 Fail 6 Total') + await expect(page.locator('[aria-labelledby=tests]')).toContainText('6 Pass 1 Fail 7 Total') // unhandled errors await expect(page.getByTestId('unhandled-errors')).toContainText( @@ -61,6 +61,9 @@ test.describe('ui', () => { await page.getByText('fixtures/console.test.ts').click() await page.getByTestId('btn-console').click() await page.getByText('/(?\\w)/').click() + + expect(await page.getByText('beforeAll').all()).toHaveLength(6) + expect(await page.getByText('afterAll').all()).toHaveLength(6) }) test('error', async ({ page }) => {