Skip to content

Commit cba2036

Browse files
authored
feat(api): expose logs recorded during the test (#10277)
1 parent 95905a8 commit cba2036

5 files changed

Lines changed: 128 additions & 5 deletions

File tree

‎docs/api/advanced/test-case.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,11 @@ function toTestSpecification(): TestSpecification
300300
```
301301

302302
Returns a new [test specification](/api/advanced/test-specification) that can be used to filter or run this specific test case.
303+
304+
## logs <Version>5.0.0</Version> {#logs}
305+
306+
```ts
307+
function logs(): ReadonlyArray<UserConsoleLog>
308+
```
309+
310+
Console logs recorded during the test execution.

‎docs/api/advanced/test-module.md‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ Module id relative to the project. This is the same as `task.name` in the deprec
3434
'project\\example.test.ts' // ❌
3535
```
3636

37+
## viteEnvironment <Version>4.1.0</Version> {#viteenvironment}
38+
39+
This is a Vite's [`DevEnvironment`](https://vite.dev/guide/api-environment) that transforms all files inside of the test module.
40+
41+
::: details History
42+
- `v4.0.15`: added as experimental
43+
:::
44+
3745
## state
3846

3947
```ts
@@ -121,13 +129,25 @@ interface ImportDuration {
121129
}
122130
```
123131
124-
## viteEnvironment <Version>4.1.0</Version> {#viteenvironment}
132+
## logs <Version>5.0.0</Version> {#logs}
125133
126-
This is a Vite's [`DevEnvironment`](https://vite.dev/guide/api-environment) that transforms all files inside of the test module.
134+
```ts
135+
function logs(): ReadonlyArray<UserConsoleLog>
136+
```
127137

128-
::: details History
129-
- `v4.0.15`: added as experimental
130-
:::
138+
Console logs recorded on top level of the module during test collection.For example:
139+
140+
```ts
141+
console.log('included') // [!code highlight]
142+
143+
describe('suite', () => {
144+
console.log('not included') // [!code error]
145+
146+
test('test', () => {
147+
console.log('not included') // [!code error]
148+
})
149+
})
150+
```
131151

132152
## toTestSpecification <Version>4.1.0</Version> {#totestspecification}
133153

‎docs/api/advanced/test-suite.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ Note that suite metadata will be inherited by tests since Vitest 4.1.
221221
If metadata was attached during collection (outside of the `test` function), then it will be available in [`onTestModuleCollected`](./reporters#ontestmodulecollected) hook in the custom reporter.
222222
:::
223223

224+
## logs <Version>5.0.0</Version> {#logs}
225+
226+
```ts
227+
function logs(): ReadonlyArray<UserConsoleLog>
228+
```
229+
230+
Console logs recorded during test collection of this suite. For example:
231+
232+
```ts
233+
describe('suite', () => {
234+
console.log('included') // [!code highlight]
235+
236+
beforeAll(() => {
237+
console.log('included') // [!code highlight]
238+
})
239+
240+
test('test', () => {
241+
console.log('not included') // [!code error]
242+
})
243+
})
244+
```
245+
224246
## toTestSpecification <Version>4.1.0</Version> {#totestspecification}
225247

226248
```ts

‎packages/vitest/src/node/reporters/reported-tasks.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
} from '@vitest/runner'
1212
import type { SerializedError, TestError } from '@vitest/utils'
1313
import type { DevEnvironment } from 'vite'
14+
import type { UserConsoleLog } from '../../types/general'
1415
import type { TestProject } from '../project'
1516
import type { TestSpecification } from '../test-specification'
1617

@@ -65,6 +66,13 @@ class ReportedTaskImplementation {
6566
return this.task.meta
6667
}
6768

69+
/**
70+
* Console logs recorded during the test execution.
71+
*/
72+
public logs(): ReadonlyArray<UserConsoleLog> {
73+
return [...this.task.logs || []]
74+
}
75+
6876
/**
6977
* Creates a new reported task instance and stores it in the project's state for future use.
7078
* @internal

‎test/e2e/test/test-meta.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,71 @@ test('empty meta object is allowed', async () => {
657657
expect(testCase.meta()).toMatchInlineSnapshot(`{}`)
658658
})
659659

660+
test('testCase, testSuite, and testModule record their logs', async () => {
661+
const { ctx } = await runInlineTests({
662+
'basic.test.js': `
663+
console.log('module log')
664+
console.error('module error')
665+
666+
describe('suite', () => {
667+
beforeAll(() => {
668+
console.log('suite log')
669+
})
670+
671+
test('test 1', () => {
672+
console.log('test log')
673+
console.error('test error')
674+
})
675+
})
676+
`,
677+
'vitest.config.js': {
678+
test: {
679+
globals: true,
680+
},
681+
},
682+
})
683+
684+
const testModule = ctx!.state.getTestModules()[0]
685+
const testSuite = testModule.children.at(0) as TestSuite
686+
const testCase = testSuite.children.at(0) as TestCase
687+
688+
const projectLogs = (logs: ReturnType<typeof testModule.logs>) =>
689+
logs.map(({ content, type }) => ({ content: content.trim(), type }))
690+
691+
expect(projectLogs(testModule.logs())).toMatchInlineSnapshot(`
692+
[
693+
{
694+
"content": "module log",
695+
"type": "stdout",
696+
},
697+
{
698+
"content": "module error",
699+
"type": "stderr",
700+
},
701+
]
702+
`)
703+
expect(projectLogs(testSuite.logs())).toMatchInlineSnapshot(`
704+
[
705+
{
706+
"content": "suite log",
707+
"type": "stdout",
708+
},
709+
]
710+
`)
711+
expect(projectLogs(testCase.logs())).toMatchInlineSnapshot(`
712+
[
713+
{
714+
"content": "test log",
715+
"type": "stdout",
716+
},
717+
{
718+
"content": "test error",
719+
"type": "stderr",
720+
},
721+
]
722+
`)
723+
})
724+
660725
test('meta inheritance across multiple files', async () => {
661726
const { stderr, ctx } = await runInlineTests({
662727
'file1.test.js': `

0 commit comments

Comments
 (0)