Skip to content

Commit

Permalink
fix(coverage): report uncovered files when re-run by enter or 'a'
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Nov 4, 2024
1 parent 5d4b382 commit 5d8d987
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
14 changes: 7 additions & 7 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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) {
Expand All @@ -726,19 +726,19 @@ export class Vitest {
})
})
}
await this.rerunFiles(files, trigger)
await this.rerunFiles(files, trigger, pattern === '')
}

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

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[]) {
Expand All @@ -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
Expand Down
50 changes: 43 additions & 7 deletions test/coverage-test/test/all.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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/**'],
Expand All @@ -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(`
[
"<process-cwd>/fixtures/src/even.ts",
"<process-cwd>/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('<process-cwd>/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('<process-cwd>/fixtures/src/untested-file.ts')
expect(files.length).toBeGreaterThanOrEqual(3)
}
})

0 comments on commit 5d8d987

Please sign in to comment.