Skip to content

Commit

Permalink
fix(vitest): logs in beforeAll and afterAll (#5288)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenghan34 committed Mar 15, 2024
1 parent 51d1d47 commit ce5ca6b
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/console.ts
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
10 changes: 9 additions & 1 deletion packages/vitest/src/runtime/runners/test.ts
Expand Up @@ -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
Expand All @@ -44,6 +48,8 @@ export class VitestTestRunner implements VitestRunner {
if (result)
await rpc().snapshotSaved(result)
}

this.workerState.current = suite.suite
}

onAfterRunTask(test: Task) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
39 changes: 39 additions & 0 deletions 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)
})
})
})
41 changes: 41 additions & 0 deletions 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`,
)
})
43 changes: 41 additions & 2 deletions 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', () => {
Expand Down Expand Up @@ -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)
})
})
})
2 changes: 1 addition & 1 deletion test/ui/test/html-report.spec.ts
Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion test/ui/test/ui.spec.ts
Expand Up @@ -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(
Expand Down Expand Up @@ -61,6 +61,9 @@ test.describe('ui', () => {
await page.getByText('fixtures/console.test.ts').click()
await page.getByTestId('btn-console').click()
await page.getByText('/(?<char>\\w)/').click()

expect(await page.getByText('beforeAll').all()).toHaveLength(6)
expect(await page.getByText('afterAll').all()).toHaveLength(6)
})

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

0 comments on commit ce5ca6b

Please sign in to comment.