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): uncovered files with allowExternal #6372

Closed
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
17 changes: 15 additions & 2 deletions test/coverage-test/test/allow-external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,41 @@ import { readCoverageMap, runVitest, test } from '../utils'
test('{ allowExternal: true } includes files outside project root', async () => {
await runVitest({
include: ['fixtures/test/allow-external-fixture.test.ts'],
coverage: { allowExternal: true, reporter: 'json', include: ['**/fixtures/**'], all: false },
coverage: {
allowExternal: true,
reporter: 'json',
include: ['**/fixtures/src/math.ts', '**/test/test-utils/fixtures/**'],
all: true,
},
})
const coverageMap = await readCoverageMap()
const files = coverageMap.files()

// File outside project root
expect(files).toContain('<project-root>/test/test-utils/fixtures/external-math.ts')

// Uncovered files outside project root should also be included
expect(files).toContain('<project-root>/test/test-utils/fixtures/uncovered.ts')
Comment on lines +20 to +21
Copy link
Member Author

Choose a reason for hiding this comment

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

This is actually working correctly already. Uncovered files are resolved from the project root:

const allFiles = await this.testExclude.glob(this.ctx.config.root)
let includedFiles = allFiles.map(file =>
resolve(this.ctx.config.root, file),
)
if (this.ctx.config.changed) {
includedFiles = (this.ctx.config.related || []).filter(file =>
includedFiles.includes(file),
)
}
const uncoveredFiles = includedFiles
.map(file => pathToFileURL(file))
.filter(file => !testedFiles.includes(file.pathname))

const allFiles = await this.testExclude.glob(this.ctx.config.root)
let includedFiles = allFiles.map(file =>
resolve(this.ctx.config.root, file),
)
if (this.ctx.config.changed) {
includedFiles = (this.ctx.config.related || []).filter(file =>
includedFiles.includes(file),
)
}
const uncoveredFiles = includedFiles
.filter(file => !coveredFiles.includes(file))
.sort()


// Files inside project root should always be included
expect(files).toContain('<process-cwd>/fixtures/src/math.ts')
})

test('{ allowExternal: false } excludes files outside project root', async () => {
await runVitest({
include: ['fixtures/test/allow-external-fixture.test.ts'],
coverage: { allowExternal: false, reporter: 'json', include: ['**/fixtures/**'] },
coverage: {
allowExternal: false,
reporter: 'json',
include: ['**/fixtures/src/math.ts', '**/test/test-utils/fixtures/**'],
},
})
const coverageMap = await readCoverageMap()
const files = coverageMap.files()

// File outside project root
expect(files.find(file => file.includes('test-utils/fixtures/external-math.ts'))).toBeFalsy()
expect(files.find(file => file.includes('test-utils/fixtures/uncovered.ts'))).toBeFalsy()

// Files inside project root should always be included
expect(files).toContain('<process-cwd>/fixtures/src/math.ts')
Expand Down
3 changes: 3 additions & 0 deletions test/test-utils/fixtures/uncovered.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function uncovered() {
return "This file is not covered"
}
Loading