From 487c80ae589ec74255e8d3a0c6bd17385c72c2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Tue, 5 Nov 2024 13:17:27 +0200 Subject: [PATCH] fix(coverage): report uncovered files when re-run by `enter` or `'a'` (#6848) --- packages/vitest/src/node/core.ts | 14 ++++---- test/coverage-test/test/all.test.ts | 50 +++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 8ce04b94823a..763fec4c468a 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -685,14 +685,14 @@ export class Vitest { await Promise.all(this._onCancelListeners.splice(0).map(listener => listener(reason))) } - async rerunFiles(files: string[] = this.state.getFilepaths(), trigger?: string) { + async rerunFiles(files: string[] = this.state.getFilepaths(), trigger?: string, allTestsRun = true) { if (this.filenamePattern) { const filteredFiles = await this.globTestFiles([this.filenamePattern]) files = files.filter(file => filteredFiles.some(f => f[1] === file)) } await this.report('onWatcherRerun', files, trigger) - await this.runFiles(files.flatMap(file => this.getProjectsByTestFile(file)), !trigger) + await this.runFiles(files.flatMap(file => this.getProjectsByTestFile(file)), allTestsRun) await this.report('onWatcherStart', this.state.getFiles(files)) } @@ -705,7 +705,7 @@ export class Vitest { this.projects = this.resolvedProjects.filter(p => p.getName() === pattern) const files = (await this.globTestSpecs()).map(spec => spec.moduleId) - await this.rerunFiles(files, 'change project filter') + await this.rerunFiles(files, 'change project filter', pattern === '') } async changeNamePattern(pattern: string, files: string[] = this.state.getFilepaths(), trigger?: string) { @@ -726,7 +726,7 @@ export class Vitest { }) }) } - await this.rerunFiles(files, trigger) + await this.rerunFiles(files, trigger, pattern === '') } async changeFilenamePattern(pattern: string, files: string[] = this.state.getFilepaths()) { @@ -734,11 +734,11 @@ export class Vitest { const trigger = this.filenamePattern ? 'change filename pattern' : 'reset filename pattern' - await this.rerunFiles(files, trigger) + await this.rerunFiles(files, trigger, pattern === '') } async rerunFailed() { - await this.rerunFiles(this.state.getFailedFilepaths(), 'rerun failed') + await this.rerunFiles(this.state.getFailedFilepaths(), 'rerun failed', false) } async updateSnapshot(files?: string[]) { @@ -755,7 +755,7 @@ export class Vitest { } try { - await this.rerunFiles(files, 'update snapshot') + await this.rerunFiles(files, 'update snapshot', false) } finally { delete this.configOverride.snapshotOptions diff --git a/test/coverage-test/test/all.test.ts b/test/coverage-test/test/all.test.ts index 97e464ed80af..7c49403fc8be 100644 --- a/test/coverage-test/test/all.test.ts +++ b/test/coverage-test/test/all.test.ts @@ -3,8 +3,7 @@ import { readCoverageMap, runVitest, test } from '../utils' test('{ all: true } includes uncovered files', async () => { await runVitest({ - include: ['fixtures/test/**'], - exclude: ['**/virtual-files-**', '**/custom-1-syntax**'], + include: ['fixtures/test/math.test.ts', 'fixtures/test/even.test.ts'], coverage: { include: ['fixtures/src/**'], all: true, @@ -24,7 +23,7 @@ test('{ all: true } includes uncovered files', async () => { test('{ all: false } excludes uncovered files', async () => { await runVitest({ - include: ['fixtures/test/**'], + include: ['fixtures/test/math.test.ts', 'fixtures/test/even.test.ts'], exclude: ['**/virtual-files-**', '**/custom-1-syntax**'], coverage: { include: ['fixtures/src/**'], @@ -36,9 +35,46 @@ test('{ all: false } excludes uncovered files', async () => { const coverageMap = await readCoverageMap() const files = coverageMap.files() - expect(files.find(file => file.includes('untested-file'))).toBeFalsy() - expect(files.length).toBeGreaterThanOrEqual(3) + // Only executed files should be present on report + expect(files).toMatchInlineSnapshot(` + [ + "/fixtures/src/even.ts", + "/fixtures/src/math.ts", + ] + `) +}) - // Directories starting with dot should be excluded, check for ".should-be-excluded-from-coverage/excluded-from-coverage.ts" - expect(files.find(file => file.includes('excluded-from-coverage'))).toBeFalsy() +test('{ all: true } includes uncovered files after watch-mode re-run', async () => { + const { vitest, ctx } = await runVitest({ + watch: true, + include: ['fixtures/test/math.test.ts', 'fixtures/test/even.test.ts'], + coverage: { + include: ['fixtures/src/**'], + all: true, + reporter: 'json', + }, + }) + + { + const coverageMap = await readCoverageMap() + const files = coverageMap.files() + + expect(files).toContain('/fixtures/src/untested-file.ts') + expect(files.length).toBeGreaterThanOrEqual(3) + } + + vitest.write('a') + + await vitest.waitForStdout('RERUN') + await vitest.waitForStdout('rerun all tests') + await vitest.waitForStdout('Waiting for file changes') + await ctx!.close() + + { + const coverageMap = await readCoverageMap() + const files = coverageMap.files() + + expect(files).toContain('/fixtures/src/untested-file.ts') + expect(files.length).toBeGreaterThanOrEqual(3) + } })