Skip to content

Commit 487c80a

Browse files
authoredNov 5, 2024··
fix(coverage): report uncovered files when re-run by enter or 'a' (#6848)
1 parent 7e1faf3 commit 487c80a

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed
 

‎packages/vitest/src/node/core.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,14 @@ export class Vitest {
685685
await Promise.all(this._onCancelListeners.splice(0).map(listener => listener(reason)))
686686
}
687687

688-
async rerunFiles(files: string[] = this.state.getFilepaths(), trigger?: string) {
688+
async rerunFiles(files: string[] = this.state.getFilepaths(), trigger?: string, allTestsRun = true) {
689689
if (this.filenamePattern) {
690690
const filteredFiles = await this.globTestFiles([this.filenamePattern])
691691
files = files.filter(file => filteredFiles.some(f => f[1] === file))
692692
}
693693

694694
await this.report('onWatcherRerun', files, trigger)
695-
await this.runFiles(files.flatMap(file => this.getProjectsByTestFile(file)), !trigger)
695+
await this.runFiles(files.flatMap(file => this.getProjectsByTestFile(file)), allTestsRun)
696696

697697
await this.report('onWatcherStart', this.state.getFiles(files))
698698
}
@@ -705,7 +705,7 @@ export class Vitest {
705705

706706
this.projects = this.resolvedProjects.filter(p => p.getName() === pattern)
707707
const files = (await this.globTestSpecs()).map(spec => spec.moduleId)
708-
await this.rerunFiles(files, 'change project filter')
708+
await this.rerunFiles(files, 'change project filter', pattern === '')
709709
}
710710

711711
async changeNamePattern(pattern: string, files: string[] = this.state.getFilepaths(), trigger?: string) {
@@ -726,19 +726,19 @@ export class Vitest {
726726
})
727727
})
728728
}
729-
await this.rerunFiles(files, trigger)
729+
await this.rerunFiles(files, trigger, pattern === '')
730730
}
731731

732732
async changeFilenamePattern(pattern: string, files: string[] = this.state.getFilepaths()) {
733733
this.filenamePattern = pattern
734734

735735
const trigger = this.filenamePattern ? 'change filename pattern' : 'reset filename pattern'
736736

737-
await this.rerunFiles(files, trigger)
737+
await this.rerunFiles(files, trigger, pattern === '')
738738
}
739739

740740
async rerunFailed() {
741-
await this.rerunFiles(this.state.getFailedFilepaths(), 'rerun failed')
741+
await this.rerunFiles(this.state.getFailedFilepaths(), 'rerun failed', false)
742742
}
743743

744744
async updateSnapshot(files?: string[]) {
@@ -755,7 +755,7 @@ export class Vitest {
755755
}
756756

757757
try {
758-
await this.rerunFiles(files, 'update snapshot')
758+
await this.rerunFiles(files, 'update snapshot', false)
759759
}
760760
finally {
761761
delete this.configOverride.snapshotOptions

‎test/coverage-test/test/all.test.ts

+43-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { readCoverageMap, runVitest, test } from '../utils'
33

44
test('{ all: true } includes uncovered files', async () => {
55
await runVitest({
6-
include: ['fixtures/test/**'],
7-
exclude: ['**/virtual-files-**', '**/custom-1-syntax**'],
6+
include: ['fixtures/test/math.test.ts', 'fixtures/test/even.test.ts'],
87
coverage: {
98
include: ['fixtures/src/**'],
109
all: true,
@@ -24,7 +23,7 @@ test('{ all: true } includes uncovered files', async () => {
2423

2524
test('{ all: false } excludes uncovered files', async () => {
2625
await runVitest({
27-
include: ['fixtures/test/**'],
26+
include: ['fixtures/test/math.test.ts', 'fixtures/test/even.test.ts'],
2827
exclude: ['**/virtual-files-**', '**/custom-1-syntax**'],
2928
coverage: {
3029
include: ['fixtures/src/**'],
@@ -36,9 +35,46 @@ test('{ all: false } excludes uncovered files', async () => {
3635
const coverageMap = await readCoverageMap()
3736
const files = coverageMap.files()
3837

39-
expect(files.find(file => file.includes('untested-file'))).toBeFalsy()
40-
expect(files.length).toBeGreaterThanOrEqual(3)
38+
// Only executed files should be present on report
39+
expect(files).toMatchInlineSnapshot(`
40+
[
41+
"<process-cwd>/fixtures/src/even.ts",
42+
"<process-cwd>/fixtures/src/math.ts",
43+
]
44+
`)
45+
})
4146

42-
// Directories starting with dot should be excluded, check for ".should-be-excluded-from-coverage/excluded-from-coverage.ts"
43-
expect(files.find(file => file.includes('excluded-from-coverage'))).toBeFalsy()
47+
test('{ all: true } includes uncovered files after watch-mode re-run', async () => {
48+
const { vitest, ctx } = await runVitest({
49+
watch: true,
50+
include: ['fixtures/test/math.test.ts', 'fixtures/test/even.test.ts'],
51+
coverage: {
52+
include: ['fixtures/src/**'],
53+
all: true,
54+
reporter: 'json',
55+
},
56+
})
57+
58+
{
59+
const coverageMap = await readCoverageMap()
60+
const files = coverageMap.files()
61+
62+
expect(files).toContain('<process-cwd>/fixtures/src/untested-file.ts')
63+
expect(files.length).toBeGreaterThanOrEqual(3)
64+
}
65+
66+
vitest.write('a')
67+
68+
await vitest.waitForStdout('RERUN')
69+
await vitest.waitForStdout('rerun all tests')
70+
await vitest.waitForStdout('Waiting for file changes')
71+
await ctx!.close()
72+
73+
{
74+
const coverageMap = await readCoverageMap()
75+
const files = coverageMap.files()
76+
77+
expect(files).toContain('<process-cwd>/fixtures/src/untested-file.ts')
78+
expect(files.length).toBeGreaterThanOrEqual(3)
79+
}
4480
})

0 commit comments

Comments
 (0)
Please sign in to comment.