Skip to content

Commit

Permalink
perf: reuse full name in reported tasks, update generator types (#6666)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Oct 8, 2024
1 parent 9936c09 commit 7a0de0a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
25 changes: 17 additions & 8 deletions packages/vitest/src/node/reporters/reported-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
TaskMeta,
} from '@vitest/runner'
import type { TestError } from '@vitest/utils'
import { getTestName } from '../../utils/tasks'
import type { WorkspaceProject } from '../workspace'
import { TestProject } from '../reported-workspace-project'

Expand Down Expand Up @@ -101,7 +100,12 @@ export class TestCase extends ReportedTaskImplementation {
*/
public get fullName(): string {
if (this.#fullName === undefined) {
this.#fullName = getTestName(this.task, ' > ')
if (this.parent.type !== 'module') {
this.#fullName = `${this.parent.fullName} > ${this.name}`
}
else {
this.#fullName = this.name
}
}
return this.#fullName
}
Expand Down Expand Up @@ -198,7 +202,7 @@ class TestCollection {
/**
* Filters all tests that are part of this collection and its children.
*/
*allTests(state?: TestResult['state'] | 'running'): IterableIterator<TestCase> {
*allTests(state?: TestResult['state'] | 'running'): Generator<TestCase, undefined, void> {
for (const child of this) {
if (child.type === 'suite') {
yield * child.children.allTests(state)
Expand All @@ -218,7 +222,7 @@ class TestCollection {
/**
* Filters only the tests that are part of this collection.
*/
*tests(state?: TestResult['state'] | 'running'): IterableIterator<TestCase> {
*tests(state?: TestResult['state'] | 'running'): Generator<TestCase, undefined, void> {
for (const child of this) {
if (child.type !== 'test') {
continue
Expand All @@ -239,7 +243,7 @@ class TestCollection {
/**
* Filters only the suites that are part of this collection.
*/
*suites(): IterableIterator<TestSuite> {
*suites(): Generator<TestSuite, undefined, void> {
for (const child of this) {
if (child.type === 'suite') {
yield child
Expand All @@ -250,7 +254,7 @@ class TestCollection {
/**
* Filters all suites that are part of this collection and its children.
*/
*allSuites(): IterableIterator<TestSuite> {
*allSuites(): Generator<TestSuite, undefined, void> {
for (const child of this) {
if (child.type === 'suite') {
yield child
Expand All @@ -259,7 +263,7 @@ class TestCollection {
}
}

*[Symbol.iterator](): IterableIterator<TestSuite | TestCase> {
*[Symbol.iterator](): Generator<TestSuite | TestCase, undefined, void> {
for (const task of this.#task.tasks) {
yield getReportedTask(this.#project, task) as TestSuite | TestCase
}
Expand Down Expand Up @@ -328,7 +332,12 @@ export class TestSuite extends SuiteImplementation {
*/
public get fullName(): string {
if (this.#fullName === undefined) {
this.#fullName = getTestName(this.task, ' > ')
if (this.parent.type !== 'module') {
this.#fullName = `${this.parent.fullName} > ${this.name}`
}
else {
this.#fullName = this.name
}
}
return this.#fullName
}
Expand Down
17 changes: 14 additions & 3 deletions test/cli/test/reported-tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ let project: WorkspaceProject
let files: RunnerTestFile[]
let testModule: TestModule

const root = resolve(__dirname, '..', 'fixtures', 'reported-tasks')

beforeAll(async () => {
const { ctx } = await runVitest({
root: resolve(__dirname, '..', 'fixtures', 'reported-tasks'),
root,
include: ['**/*.test.ts'],
reporters: [
'verbose',
Expand Down Expand Up @@ -52,7 +54,7 @@ it('correctly reports a file', () => {
expect(testModule.task).toBe(files[0])
expect(testModule.id).toBe(files[0].id)
expect(testModule.location).toBeUndefined()
expect(testModule.moduleId).toBe(resolve('./fixtures/reported-tasks/1_first.test.ts'))
expect(testModule.moduleId).toBe(resolve(root, './1_first.test.ts'))
expect(testModule.project.workspaceProject).toBe(project)
expect(testModule.children.size).toBe(14)

Expand Down Expand Up @@ -139,7 +141,7 @@ it('correctly reports failed test', () => {
stacks: [
{
column: 13,
file: resolve('./fixtures/reported-tasks/1_first.test.ts'),
file: resolve(root, './1_first.test.ts'),
line: 10,
method: '',
},
Expand Down Expand Up @@ -217,6 +219,15 @@ it('correctly passed down metadata', () => {
expect(meta).toHaveProperty('key', 'value')
})

it('correctly builds the full name', () => {
const suiteTopLevel = testModule.children.suites().next().value!
const suiteSecondLevel = suiteTopLevel.children.suites().next().value!
const test = suiteSecondLevel.children.at(0) as TestCase
expect(test.fullName).toBe('a group > a nested group > runs a test in a nested group')
expect(suiteTopLevel.fullName).toBe('a group')
expect(suiteSecondLevel.fullName).toBe('a group > a nested group')
})

function date(time: Date) {
return `${time.getDate()}/${time.getMonth() + 1}/${time.getFullYear()}`
}
Expand Down
1 change: 1 addition & 0 deletions vitest.workspace.vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { defineWorkspace } from 'vitest/config'

export default defineWorkspace([
'./test/core',
'./test/cli',
])

0 comments on commit 7a0de0a

Please sign in to comment.