Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(coverage): report uncovered files when re-run by enter or 'a' #6848

Merged
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
14 changes: 7 additions & 7 deletions packages/vitest/src/node/core.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to resolve value of allTestsRun here:

.finally(async () => {
// can be duplicate files if different projects are using the same file
const files = Array.from(new Set(specs.map(spec => spec.moduleId)))
const errors = this.state.getUnhandledErrors()
const coverage = await this.coverageProvider?.generateCoverage({ allTestsRun })

... based on all current filters. We could check this.state.getFiles().length === specs.length and check testNamePattern maybe..?

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)
}
})