Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export class Vitest {

/** @internal */ configOverride: Partial<ResolvedConfig> = {}
/** @internal */ filenamePattern?: string[]
/** @internal */ _sourceFilterResult?: { affected: number; total: number }
/** @internal */ runningPromise?: Promise<TestRunResult>
/** @internal */ closingPromise?: Promise<void>
/** @internal */ cancelPromise?: Promise<void | void[]>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
formatTimeString,
getStateString,
getStateSymbol,
noun,
padSummaryTitle,
renderSnapshotSummary,
separator,
Expand Down Expand Up @@ -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()

Expand All @@ -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)) {
Expand Down
8 changes: 7 additions & 1 deletion packages/vitest/src/node/specifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export class VitestSpecifications {
}

private async filterTestsBySource(specs: TestSpecification[]): Promise<TestSpecification[]> {
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,
Expand Down Expand Up @@ -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
}

/**
Expand Down
47 changes: 46 additions & 1 deletion test/e2e/test/vcs-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 () => {
Expand Down
Loading