diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 4f2659f5f423..9f005d4acb07 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -151,6 +151,7 @@ export class Vitest { /** @internal */ configOverride: Partial = {} /** @internal */ filenamePattern?: string[] + /** @internal */ _sourceFilterResult?: { affected: number; total: number } /** @internal */ runningPromise?: Promise /** @internal */ closingPromise?: Promise /** @internal */ cancelPromise?: Promise @@ -1102,6 +1103,7 @@ export class Vitest { // all subsequent runs will treat this as a fresh run this.config.changed = false this.config.related = undefined + this._sourceFilterResult = undefined }) return await this.runningPromise @@ -1301,6 +1303,7 @@ export class Vitest { // all subsequent runs will treat this as a fresh run this.config.changed = false this.config.related = undefined + this._sourceFilterResult = undefined }) return await this.runningPromise diff --git a/packages/vitest/src/node/reporters/base.ts b/packages/vitest/src/node/reporters/base.ts index 25a7ac167fe1..076305b469b5 100644 --- a/packages/vitest/src/node/reporters/base.ts +++ b/packages/vitest/src/node/reporters/base.ts @@ -54,6 +54,7 @@ import { formatTimeString, getStateString, getStateSymbol, + noun, padSummaryTitle, renderSnapshotSummary, separator, @@ -659,6 +660,25 @@ export abstract class BaseReporter implements Reporter { this.reportTestSummary(files, errors, leakCount) } + private printSourceFilterSummary(): void { + const result = this.ctx._sourceFilterResult + const related = this.ctx.config.related + if (!result || !related) { + return + } + + const sources = + related.length === 1 && !this.ctx.config.changed + ? relative(this.ctx.config.root, related[0]) + : `${related.length} ${this.ctx.config.changed ? 'changed ' : ''}${noun(related.length, 'file', 'files')}` + + this.log( + padSummaryTitle('Affected'), + `${result.affected} of ${result.total} test files`, + c.dim(`(related to ${sources})`), + ) + } + reportTestSummary(files: File[], errors: unknown[], leakCount: number): void { this.log() @@ -677,6 +697,7 @@ export abstract class BaseReporter implements Reporter { } this.log(padSummaryTitle('Test Files'), getStateString(affectedFiles)) + this.printSourceFilterSummary() this.log(padSummaryTitle('Tests'), getStateString(tests)) if (this.ctx.projects.some((c) => c.config.typecheck.enabled)) { diff --git a/packages/vitest/src/node/specifications.ts b/packages/vitest/src/node/specifications.ts index da5bb15cc15b..e2af1b4441c5 100644 --- a/packages/vitest/src/node/specifications.ts +++ b/packages/vitest/src/node/specifications.ts @@ -117,6 +117,8 @@ export class VitestSpecifications { } private async filterTestsBySource(specs: TestSpecification[]): Promise { + this.vitest._sourceFilterResult = undefined + if (this.vitest.config.changed && !this.vitest.config.related) { const related = await this.vitest.vcs.findChangedFiles({ root: this.vitest.config.root, @@ -157,7 +159,11 @@ export class VitestSpecifications { affectedByProject.set(project, await this.getAffectedModules(project, projectSpecs, related)) } - return specs.filter((spec) => affectedByProject.get(spec.project)!.has(spec.moduleId)) + const affectedSpecs = specs.filter((spec) => + affectedByProject.get(spec.project)!.has(spec.moduleId), + ) + this.vitest._sourceFilterResult = { affected: affectedSpecs.length, total: specs.length } + return affectedSpecs } /** diff --git a/test/e2e/test/vcs-provider.test.ts b/test/e2e/test/vcs-provider.test.ts index 5097f6a92980..7b49f064c685 100644 --- a/test/e2e/test/vcs-provider.test.ts +++ b/test/e2e/test/vcs-provider.test.ts @@ -4,8 +4,12 @@ import { expect, onTestFinished, test, vi } from 'vitest' import { createVitest } from 'vitest/node' import { runInlineTests, useFS } from '#test-utils' +function affectedSummary(stdout: string) { + return stdout.split('\n').find((line) => line.includes('Affected')) +} + test('custom vcsProvider that returns specific files runs only matching tests', async () => { - const { testTree, stderr } = await runInlineTests( + const { testTree, stderr, stdout } = await runInlineTests( { 'vitest.config.js': ` import path from 'node:path' @@ -51,6 +55,47 @@ test('custom vcsProvider that returns specific files runs only matching tests', }, } `) + expect(affectedSummary(stdout)).toMatchInlineSnapshot( + `" Affected 1 of 2 test files (related to 1 changed file)"`, + ) +}) + +test('related prints how many test files were affected', async () => { + const { testTree, stderr, stdout } = await runInlineTests( + { + 'src/changed.ts': 'export const a = 1', + 'src/not-changed.ts': 'export const b = 2', + 'related.test.ts': ` + import { a } from './src/changed.ts' + import { test, expect } from 'vitest' + test('related test', () => { + expect(a).toBe(1) + }) + `, + 'not-related.test.ts': ` + import { b } from './src/not-changed.ts' + import { test, expect } from 'vitest' + test('not related test', () => { + expect(b).toBe(2) + }) + `, + }, + { + related: ['src/changed.ts'], + }, + ) + + expect(stderr).toBe('') + expect(testTree()).toMatchInlineSnapshot(` + { + "related.test.ts": { + "related test": "passed", + }, + } + `) + expect(affectedSummary(stdout)).toMatchInlineSnapshot( + `" Affected 1 of 2 test files (related to src/changed.ts)"`, + ) }) test('custom vcsProvider that returns no files runs no tests', async () => {