Skip to content

Commit

Permalink
fix(coverage): consistent type-only file handling (#6183)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Jul 22, 2024
1 parent 7a75bd4 commit 9057614
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 33 deletions.
1 change: 0 additions & 1 deletion packages/coverage-v8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"magic-string": "^0.30.10",
"magicast": "^0.3.4",
"std-env": "^3.7.0",
"strip-literal": "^2.1.0",
"test-exclude": "^7.0.1",
"tinyrainbow": "^1.2.0"
},
Expand Down
8 changes: 1 addition & 7 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import remapping from '@ampproject/remapping'
import { normalize, resolve } from 'pathe'
import c from 'tinyrainbow'
import { provider } from 'std-env'
import { stripLiteral } from 'strip-literal'
import createDebug from 'debug'
import { cleanUrl } from 'vite-node/utils'
import type { EncodedSourceMap, FetchResult } from 'vite-node'
Expand Down Expand Up @@ -391,16 +390,11 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage

const coverages = await Promise.all(
chunk.map(async (filename) => {
const { originalSource, source } = await this.getSources(
const { originalSource } = await this.getSources(
filename.href,
transformResults,
)

// Ignore empty files, e.g. files that contain only typescript types and no runtime code
if (source && stripLiteral(source).trim() === '') {
return null
}

const coverage = {
url: filename.href,
scriptId: '0',
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 9 additions & 22 deletions test/coverage-test/test/empty-lines.v8.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type LineCoverage = Record<number, CoveredLine | UncoveredLine | IgnoredLine>
describe('include empty lines', () => {
let coveredFileLines: LineCoverage
let uncoveredFileLines: LineCoverage
let files: string[]

beforeAll(async () => {
await runVitest({
Expand All @@ -28,16 +27,7 @@ describe('include empty lines', () => {
},
})

;({ coveredFileLines, uncoveredFileLines, files } = await readCoverage())
})

test('file containing only types is ignored', () => {
expect(files).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/src/empty-lines.ts",
"<process-cwd>/fixtures/src/untested-file.ts",
]
`)
;({ coveredFileLines, uncoveredFileLines } = await readCoverage())
})

test('lines are included', async () => {
Expand All @@ -64,7 +54,7 @@ describe('include empty lines', () => {
describe('ignore empty lines', () => {
let coveredFileLines: LineCoverage
let uncoveredFileLines: LineCoverage
let files: string[]
let typesOnlyFileLines: LineCoverage

beforeAll(async () => {
await runVitest({
Expand All @@ -79,16 +69,13 @@ describe('ignore empty lines', () => {
},
})

;({ coveredFileLines, uncoveredFileLines, files } = await readCoverage())
;({ coveredFileLines, uncoveredFileLines, typesOnlyFileLines } = await readCoverage())
})

test('file containing only types is ignored', () => {
expect(files).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/src/empty-lines.ts",
"<process-cwd>/fixtures/src/untested-file.ts",
]
`)
test('file containing only types has no uncovered lines', () => {
expect(typesOnlyFileLines[1]).toBe(undefined)
expect(typesOnlyFileLines[2]).toBe(undefined)
expect(typesOnlyFileLines[3]).toBe(undefined)
})

test('empty lines are ignored', async () => {
Expand Down Expand Up @@ -184,12 +171,12 @@ coverageTest('cover some lines', () => {

async function readCoverage() {
const coverageMap = await readCoverageMap()
const files = coverageMap.files()

const coveredFileLines = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/empty-lines.ts').getLineCoverage() as LineCoverage
const uncoveredFileLines = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/untested-file.ts').getLineCoverage() as LineCoverage
const typesOnlyFileLines = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/types-only.ts').getLineCoverage() as LineCoverage

return { coveredFileLines, uncoveredFileLines, files }
return { coveredFileLines, uncoveredFileLines, typesOnlyFileLines }
}

function range(count: number, options: { base: number } = { base: 1 }) {
Expand Down

0 comments on commit 9057614

Please sign in to comment.