diff --git a/packages/vitest/src/node/reporters/reported-tasks.ts b/packages/vitest/src/node/reporters/reported-tasks.ts index 593083cda500..6914e2cf6165 100644 --- a/packages/vitest/src/node/reporters/reported-tasks.ts +++ b/packages/vitest/src/node/reporters/reported-tasks.ts @@ -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' @@ -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 } @@ -198,7 +202,7 @@ class TestCollection { /** * Filters all tests that are part of this collection and its children. */ - *allTests(state?: TestResult['state'] | 'running'): IterableIterator { + *allTests(state?: TestResult['state'] | 'running'): Generator { for (const child of this) { if (child.type === 'suite') { yield * child.children.allTests(state) @@ -218,7 +222,7 @@ class TestCollection { /** * Filters only the tests that are part of this collection. */ - *tests(state?: TestResult['state'] | 'running'): IterableIterator { + *tests(state?: TestResult['state'] | 'running'): Generator { for (const child of this) { if (child.type !== 'test') { continue @@ -239,7 +243,7 @@ class TestCollection { /** * Filters only the suites that are part of this collection. */ - *suites(): IterableIterator { + *suites(): Generator { for (const child of this) { if (child.type === 'suite') { yield child @@ -250,7 +254,7 @@ class TestCollection { /** * Filters all suites that are part of this collection and its children. */ - *allSuites(): IterableIterator { + *allSuites(): Generator { for (const child of this) { if (child.type === 'suite') { yield child @@ -259,7 +263,7 @@ class TestCollection { } } - *[Symbol.iterator](): IterableIterator { + *[Symbol.iterator](): Generator { for (const task of this.#task.tasks) { yield getReportedTask(this.#project, task) as TestSuite | TestCase } @@ -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 } diff --git a/test/cli/test/reported-tasks.test.ts b/test/cli/test/reported-tasks.test.ts index 29f278c1f370..f7f29aec7dd7 100644 --- a/test/cli/test/reported-tasks.test.ts +++ b/test/cli/test/reported-tasks.test.ts @@ -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', @@ -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) @@ -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: '', }, @@ -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()}` } diff --git a/vitest.workspace.vscode.ts b/vitest.workspace.vscode.ts index 4e113ee28243..d6a950913e66 100644 --- a/vitest.workspace.vscode.ts +++ b/vitest.workspace.vscode.ts @@ -2,4 +2,5 @@ import { defineWorkspace } from 'vitest/config' export default defineWorkspace([ './test/core', + './test/cli', ])