Skip to content

Commit 7a0de0a

Browse files
authored
perf: reuse full name in reported tasks, update generator types (#6666)
1 parent 9936c09 commit 7a0de0a

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
TaskMeta,
88
} from '@vitest/runner'
99
import type { TestError } from '@vitest/utils'
10-
import { getTestName } from '../../utils/tasks'
1110
import type { WorkspaceProject } from '../workspace'
1211
import { TestProject } from '../reported-workspace-project'
1312

@@ -101,7 +100,12 @@ export class TestCase extends ReportedTaskImplementation {
101100
*/
102101
public get fullName(): string {
103102
if (this.#fullName === undefined) {
104-
this.#fullName = getTestName(this.task, ' > ')
103+
if (this.parent.type !== 'module') {
104+
this.#fullName = `${this.parent.fullName} > ${this.name}`
105+
}
106+
else {
107+
this.#fullName = this.name
108+
}
105109
}
106110
return this.#fullName
107111
}
@@ -198,7 +202,7 @@ class TestCollection {
198202
/**
199203
* Filters all tests that are part of this collection and its children.
200204
*/
201-
*allTests(state?: TestResult['state'] | 'running'): IterableIterator<TestCase> {
205+
*allTests(state?: TestResult['state'] | 'running'): Generator<TestCase, undefined, void> {
202206
for (const child of this) {
203207
if (child.type === 'suite') {
204208
yield * child.children.allTests(state)
@@ -218,7 +222,7 @@ class TestCollection {
218222
/**
219223
* Filters only the tests that are part of this collection.
220224
*/
221-
*tests(state?: TestResult['state'] | 'running'): IterableIterator<TestCase> {
225+
*tests(state?: TestResult['state'] | 'running'): Generator<TestCase, undefined, void> {
222226
for (const child of this) {
223227
if (child.type !== 'test') {
224228
continue
@@ -239,7 +243,7 @@ class TestCollection {
239243
/**
240244
* Filters only the suites that are part of this collection.
241245
*/
242-
*suites(): IterableIterator<TestSuite> {
246+
*suites(): Generator<TestSuite, undefined, void> {
243247
for (const child of this) {
244248
if (child.type === 'suite') {
245249
yield child
@@ -250,7 +254,7 @@ class TestCollection {
250254
/**
251255
* Filters all suites that are part of this collection and its children.
252256
*/
253-
*allSuites(): IterableIterator<TestSuite> {
257+
*allSuites(): Generator<TestSuite, undefined, void> {
254258
for (const child of this) {
255259
if (child.type === 'suite') {
256260
yield child
@@ -259,7 +263,7 @@ class TestCollection {
259263
}
260264
}
261265

262-
*[Symbol.iterator](): IterableIterator<TestSuite | TestCase> {
266+
*[Symbol.iterator](): Generator<TestSuite | TestCase, undefined, void> {
263267
for (const task of this.#task.tasks) {
264268
yield getReportedTask(this.#project, task) as TestSuite | TestCase
265269
}
@@ -328,7 +332,12 @@ export class TestSuite extends SuiteImplementation {
328332
*/
329333
public get fullName(): string {
330334
if (this.#fullName === undefined) {
331-
this.#fullName = getTestName(this.task, ' > ')
335+
if (this.parent.type !== 'module') {
336+
this.#fullName = `${this.parent.fullName} > ${this.name}`
337+
}
338+
else {
339+
this.#fullName = this.name
340+
}
332341
}
333342
return this.#fullName
334343
}

test/cli/test/reported-tasks.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ let project: WorkspaceProject
1414
let files: RunnerTestFile[]
1515
let testModule: TestModule
1616

17+
const root = resolve(__dirname, '..', 'fixtures', 'reported-tasks')
18+
1719
beforeAll(async () => {
1820
const { ctx } = await runVitest({
19-
root: resolve(__dirname, '..', 'fixtures', 'reported-tasks'),
21+
root,
2022
include: ['**/*.test.ts'],
2123
reporters: [
2224
'verbose',
@@ -52,7 +54,7 @@ it('correctly reports a file', () => {
5254
expect(testModule.task).toBe(files[0])
5355
expect(testModule.id).toBe(files[0].id)
5456
expect(testModule.location).toBeUndefined()
55-
expect(testModule.moduleId).toBe(resolve('./fixtures/reported-tasks/1_first.test.ts'))
57+
expect(testModule.moduleId).toBe(resolve(root, './1_first.test.ts'))
5658
expect(testModule.project.workspaceProject).toBe(project)
5759
expect(testModule.children.size).toBe(14)
5860

@@ -139,7 +141,7 @@ it('correctly reports failed test', () => {
139141
stacks: [
140142
{
141143
column: 13,
142-
file: resolve('./fixtures/reported-tasks/1_first.test.ts'),
144+
file: resolve(root, './1_first.test.ts'),
143145
line: 10,
144146
method: '',
145147
},
@@ -217,6 +219,15 @@ it('correctly passed down metadata', () => {
217219
expect(meta).toHaveProperty('key', 'value')
218220
})
219221

222+
it('correctly builds the full name', () => {
223+
const suiteTopLevel = testModule.children.suites().next().value!
224+
const suiteSecondLevel = suiteTopLevel.children.suites().next().value!
225+
const test = suiteSecondLevel.children.at(0) as TestCase
226+
expect(test.fullName).toBe('a group > a nested group > runs a test in a nested group')
227+
expect(suiteTopLevel.fullName).toBe('a group')
228+
expect(suiteSecondLevel.fullName).toBe('a group > a nested group')
229+
})
230+
220231
function date(time: Date) {
221232
return `${time.getDate()}/${time.getMonth() + 1}/${time.getFullYear()}`
222233
}

vitest.workspace.vscode.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import { defineWorkspace } from 'vitest/config'
22

33
export default defineWorkspace([
44
'./test/core',
5+
'./test/cli',
56
])

0 commit comments

Comments
 (0)