Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vitest): logs in beforeAll and afterAll #5288

Merged
merged 4 commits into from
Mar 15, 2024
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
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/console.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
}

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading