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): trim URL parameters from file paths in c8 coverage #2181

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/coverage-c8/src/provider.ts
Expand Up @@ -58,7 +58,7 @@ export class C8CoverageProvider implements CoverageProvider {
if (!map)
return

const url = _url.pathToFileURL(file).href
const url = _url.pathToFileURL(file.split('?')[0]).href

let code: string | undefined
try {
Expand Down Expand Up @@ -86,7 +86,7 @@ export class C8CoverageProvider implements CoverageProvider {
const offset = 224

report._getSourceMap = (coverage: Profiler.ScriptCoverage) => {
const path = _url.pathToFileURL(coverage.url).href
const path = _url.pathToFileURL(coverage.url.split('?')[0]).href
const data = sourceMapMeta[path]

if (!data)
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

14 changes: 14 additions & 0 deletions test/coverage-test/coverage-test/c8/coverage-report/SFC.test.ts
@@ -0,0 +1,14 @@
/**
* @vitest-environment happy-dom
*/

import { mount } from '@vue/test-utils'
import { expect, test } from 'vitest'
import SFC from '../../../src/coverage-report/SFC.vue'

test('Should update count', async () => {
const wrapper = mount(SFC)

await wrapper.find('button').trigger('click')
expect(wrapper.text()).contain(1)
})
10 changes: 10 additions & 0 deletions test/coverage-test/coverage-test/c8/coverage-report/math.test.ts
@@ -0,0 +1,10 @@
import { expect, test } from 'vitest'
import { add, multiply } from '../../../src/coverage-report/math'

test('add', () => {
expect(add(10, 15)).toBe(25)
})

test('multiply', () => {
expect(multiply(2, 5)).toBe(10)
})
@@ -0,0 +1,14 @@
/**
* @vitest-environment happy-dom
*/

import { mount } from '@vue/test-utils'
import { expect, test } from 'vitest'
import notSFC from '../../../../src/coverage-report/not-SFC/not-SFC.vue'

test('Should update count', async () => {
const wrapper = mount(notSFC)

await wrapper.find('button').trigger('click')
expect(wrapper.text()).contain(1)
})
@@ -0,0 +1,6 @@
import { expect, test } from 'vitest'
import { add } from '../../../src/coverage-report/utils.js'

test('add', () => {
expect(add(10, 15)).toBe(25)
})
31 changes: 31 additions & 0 deletions test/coverage-test/coverage-test/coverage.c8.test.ts
@@ -1,11 +1,42 @@
import fs from 'fs'
import { execa } from 'execa'
import { resolve } from 'pathe'
import { expect, test } from 'vitest'

async function run(...runOptions: string[]): Promise<string> {
const root = resolve(__dirname, '..')

const { stdout } = await execa('npx', ['vitest', 'run', ...runOptions], {
cwd: root,
env: {
...process.env,
CI: 'true',
NO_COLOR: 'true',
},
windowsHide: false,
})

return stdout
}

test('coverage c8', async () => {
const coveragePath = resolve('./coverage/tmp/')
const stat = fs.statSync(coveragePath)
expect(stat.isDirectory()).toBe(true)
const files = fs.readdirSync(coveragePath)
expect(files.length > 0).toBe(true)
})

test('Should show coverage', async () => {
const stdout = await run('--config', 'vitest.config-c8-internal-coverage.ts', '--coverage')

// For Vue SFC and vue + ts files
expect(stdout).toContain('not-SFC.ts')
expect(stdout).not.toContain('not-SFC.ts?vue')
expect(stdout).toContain('not-SFC.vue')
expect(stdout).toContain('SFC.vue')

// For ts and js files
expect(stdout).toContain('math.ts')
expect(stdout).toContain('utils.js')
}, 30000)
1 change: 1 addition & 0 deletions test/coverage-test/package.json
Expand Up @@ -19,6 +19,7 @@
"devDependencies": {
"@vitejs/plugin-vue": "latest",
"@vue/test-utils": "latest",
"execa": "^6.1.0",
"happy-dom": "latest",
"vite": "latest",
"vitest": "workspace:*",
Expand Down
17 changes: 17 additions & 0 deletions test/coverage-test/src/coverage-report/SFC.vue
@@ -0,0 +1,17 @@
<script lang="ts">
import { defineComponent, ref } from 'vue'

export default defineComponent({
setup() {
const count = ref(0)

return { count }
},
})
</script>

<template>
<button @click="count++">
{{ count }}
</button>
</template>
15 changes: 15 additions & 0 deletions test/coverage-test/src/coverage-report/math.ts
@@ -0,0 +1,15 @@
export function add(a: number, b: number) {
return a + b
}

export function multiply(a: number, b: number) {
return a * b
}

export function untestedFn(a: number, b: number) {
return a * b
}

export function untestedFn2(a: number, b: number) {
return a * b
}
9 changes: 9 additions & 0 deletions test/coverage-test/src/coverage-report/not-SFC/not-SFC.ts
@@ -0,0 +1,9 @@
import { defineComponent, ref } from 'vue'

export default defineComponent({
setup() {
const count = ref(0)

return { count }
},
})
7 changes: 7 additions & 0 deletions test/coverage-test/src/coverage-report/not-SFC/not-SFC.vue
@@ -0,0 +1,7 @@
<script lang="ts" src="./not-SFC.ts"></script>

<template>
<button @click="count++">
{{ count }}
</button>
</template>
3 changes: 3 additions & 0 deletions test/coverage-test/src/coverage-report/utils.js
@@ -0,0 +1,3 @@
export function add(a, b) {
return a + b
}
7 changes: 7 additions & 0 deletions test/coverage-test/vitest.config-c8-coverage.ts
@@ -1,9 +1,16 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
plugins: [vue()],
test: {
include: [
'./coverage-test/*.c8.test.ts',
],
coverage: {
reporter: ['html', 'text', 'lcov'],
include: ['src/**'],
extension: ['.ts', '.vue', '.js'],
},
},
})
16 changes: 16 additions & 0 deletions test/coverage-test/vitest.config-c8-internal-coverage.ts
@@ -0,0 +1,16 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
plugins: [vue()],
test: {
include: [
'./coverage-test/c8/**/*test.{ts,js}',
],
coverage: {
reporter: ['html', 'text', 'lcov'],
include: ['src/**'],
extension: ['.ts', '.vue', '.js'],
},
},
})
2 changes: 1 addition & 1 deletion test/coverage-test/vitest.config.ts
Expand Up @@ -14,7 +14,7 @@ export default defineConfig({
'test/*.test.ts',
],
exclude: [
'coverage-test/*.test.ts',
'coverage-test/**/*',
],
coverage: {
enabled: true,
Expand Down