Skip to content

Commit

Permalink
test: Add tests on c8 coverage report
Browse files Browse the repository at this point in the history
  • Loading branch information
OrestHk committed Oct 25, 2022
1 parent e86e634 commit 1a230a4
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 1 deletion.
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-coverage.ts', '--coverage')

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

// For ts and js files
expect(stdout).contain('math.ts')
expect(stdout).contain('utils.js')
})
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-test/c8/**/*test.ts',
],
coverage: {
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

0 comments on commit 1a230a4

Please sign in to comment.