Skip to content

Commit

Permalink
fix(coverage): v8 sourcemaps with multiple sources
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 14, 2024
1 parent f44cc91 commit 5dc8208
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/config/test/exec-args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ test.each([
const root = './fixtures/exec-args-fixtures'
const fileToTest = `${pool}.test.ts`

if (process.version === 'v20.15.0' && pool !== 'forks') {
return
}

const vitest = await runVitest({
root,
include: [fileToTest],
Expand Down
30 changes: 30 additions & 0 deletions test/coverage-test/fixtures/src/pre-bundle/bundle.js

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

17 changes: 17 additions & 0 deletions test/coverage-test/fixtures/src/pre-bundle/bundle.js.map

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

2 changes: 2 additions & 0 deletions test/coverage-test/fixtures/src/pre-bundle/bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as first from "./first";
export * as second from "./second";
7 changes: 7 additions & 0 deletions test/coverage-test/fixtures/src/pre-bundle/first.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function covered() {
return "First";
}

export function uncovered() {
return "Uncovered";
}
7 changes: 7 additions & 0 deletions test/coverage-test/fixtures/src/pre-bundle/second.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function covered() {
return "Second";
}

export function uncovered() {
return "Uncovered";
}
168 changes: 168 additions & 0 deletions test/coverage-test/test/__snapshots__/bundled-istanbul.snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
{
"<process-cwd>/fixtures/src/pre-bundle/first.ts": {
"path": "<process-cwd>/fixtures/src/pre-bundle/first.ts",
"statementMap": {
"0": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 17
}
},
"1": {
"start": {
"line": 6,
"column": 2
},
"end": {
"line": 6,
"column": 21
}
}
},
"fnMap": {
"0": {
"name": "covered$1",
"decl": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 23
}
},
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 3,
"column": 1
}
}
},
"1": {
"name": "uncovered$1",
"decl": {
"start": {
"line": 5,
"column": 16
},
"end": {
"line": 5,
"column": 25
}
},
"loc": {
"start": {
"line": 5,
"column": 28
},
"end": {
"line": 7,
"column": null
}
}
}
},
"branchMap": {},
"s": {
"0": 1,
"1": 0
},
"f": {
"0": 1,
"1": 0
},
"b": {}
},
"<process-cwd>/fixtures/src/pre-bundle/second.ts": {
"path": "<process-cwd>/fixtures/src/pre-bundle/second.ts",
"statementMap": {
"0": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 18
}
},
"1": {
"start": {
"line": 6,
"column": 2
},
"end": {
"line": 6,
"column": 21
}
}
},
"fnMap": {
"0": {
"name": "covered",
"decl": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 23
}
},
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 3,
"column": 1
}
}
},
"1": {
"name": "uncovered",
"decl": {
"start": {
"line": 5,
"column": 16
},
"end": {
"line": 5,
"column": 25
}
},
"loc": {
"start": {
"line": 5,
"column": 28
},
"end": {
"line": 7,
"column": null
}
}
}
},
"branchMap": {},
"s": {
"0": 1,
"1": 0
},
"f": {
"0": 1,
"1": 0
},
"b": {}
}
}
36 changes: 36 additions & 0 deletions test/coverage-test/test/bundled-sources.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import libCoverage from 'istanbul-lib-coverage'
import { expect } from 'vitest'
import { coverageTest, isV8Provider, normalizeURL, readCoverageJson, runVitest, test } from '../utils.js'
import * as transpiled from '../fixtures/src/pre-bundle/bundle.js'

test('bundled code with source maps to originals', async () => {
await runVitest({
include: [normalizeURL(import.meta.url)],
coverage: {
include: ['fixtures/src/**'],
reporter: 'json',
all: false,
},
})

const coverageJson = await readCoverageJson()
const coverageMap = libCoverage.createCoverageMap(coverageJson)
const files = coverageMap.files()

expect(files).toContain('<process-cwd>/fixtures/src/pre-bundle/first.ts')
expect(files).toContain('<process-cwd>/fixtures/src/pre-bundle/second.ts')
expect(files.find(file => file.includes('bundle.js'))).toBeFalsy()
expect(files.find(file => file.includes('bundle.js.map'))).toBeFalsy()
expect(files.find(file => file.includes('bundle.ts'))).toBeFalsy()
expect(files.find(file => file.includes('bundle.d.ts'))).toBeFalsy()

expect(JSON.stringify(coverageJson, null, 2)).toMatchFileSnapshot(`__snapshots__/bundled-${isV8Provider() ? 'v8' : 'istanbul'}.snapshot.json`)
})

coverageTest('run bundled sources', () => {
expect(transpiled.first.covered).toBeTypeOf('function')
expect(transpiled.first.covered()).toBe('First')

expect(transpiled.second.covered).toBeTypeOf('function')
expect(transpiled.second.covered()).toBe('Second')
})

0 comments on commit 5dc8208

Please sign in to comment.