-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
generic.report.test.ts
143 lines (106 loc) · 4.52 KB
/
generic.report.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Test cases shared by both coverage providers
*/
import fs from 'node:fs'
import { resolve } from 'pathe'
import { expect, test } from 'vitest'
import libCoverage from 'istanbul-lib-coverage'
import { readCoverageJson } from './utils'
test('html report', async () => {
const coveragePath = resolve('./coverage/src')
const files = fs.readdirSync(coveragePath)
expect(files).toContain('index.html')
expect(files).toContain('index.mts.html')
expect(files).toContain('Hello.vue.html')
})
test('lcov report', async () => {
const coveragePath = resolve('./coverage')
const files = fs.readdirSync(coveragePath)
expect(files).toContain('lcov.info')
const lcovReport = resolve('./coverage/lcov-report')
const lcovReportFiles = fs.readdirSync(lcovReport)
expect(lcovReportFiles).toContain('index.html')
})
test('all includes untested files', () => {
const coveragePath = resolve('./coverage/src')
const files = fs.readdirSync(coveragePath)
expect(files).toContain('untested-file.ts.html')
})
test('files should not contain query parameters', () => {
const coveragePath = resolve('./coverage/src/Counter')
const files = fs.readdirSync(coveragePath)
expect(files).toContain('index.html')
expect(files).toContain('Counter.vue.html')
expect(files).toContain('Counter.component.ts.html')
expect(files).not.toContain('Counter.component.ts?vue&type=script&src=true&lang.ts.html')
})
test('file using import.meta.env is included in report', async () => {
const coveragePath = resolve('./coverage/src')
const files = fs.readdirSync(coveragePath)
expect(files).toContain('importEnv.ts.html')
})
test('files should not contain a setup file', () => {
const coveragePath = resolve('./coverage')
const files = fs.readdirSync(coveragePath)
expect(files).not.toContain('coverage-test')
expect(files).not.toContain('setup.ts.html')
const coverageSrcPath = resolve('./coverage/src')
const srcFiles = fs.readdirSync(coverageSrcPath)
expect(srcFiles).not.toContain('another-setup.ts.html')
})
test('thresholdAutoUpdate updates thresholds', async () => {
const configFilename = resolve('./vitest.config.ts')
const configContents = fs.readFileSync(configFilename, 'utf-8')
for (const threshold of ['functions', 'branches', 'lines', 'statements']) {
const match = configContents.match(new RegExp(`${threshold}: (?<coverage>[\\d|\\.]+)`))
const coverage = match?.groups?.coverage || '0'
// Configuration has fixed value of 1.01 and 0 set for each threshold
expect(Number.parseInt(coverage)).toBeGreaterThan(1.01)
}
// Update thresholds back to fixed values
const updatedConfig = configContents
.replace(/(branches|statements): ([\d|\.])+/g, '$1: 1.01')
.replace(/(functions|lines): ([\d|\.])+/g, '$1: 0')
fs.writeFileSync(configFilename, updatedConfig)
})
test('function count is correct', async () => {
const coverageJson = await readCoverageJson()
const coverageMap = libCoverage.createCoverageMap(coverageJson as any)
const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/src/function-count.ts')
const { functions } = fileCoverage.toSummary()
expect(functions.total).toBe(5)
expect(functions.covered).toBe(3)
})
test('coverage provider does not conflict with built-in reporter\'s outputFile', async () => {
const coveragePath = resolve('./coverage')
const files = fs.readdirSync(coveragePath)
expect(files).toContain('junit.xml')
})
test('virtual files should be excluded', () => {
const files = fs.readdirSync(resolve('./coverage'))
const srcFiles = fs.readdirSync(resolve('./coverage/src'))
for (const file of [...files, ...srcFiles]) {
expect(file).not.toContain('virtual:')
// Vitest in node
expect(file).not.toContain('__x00__')
expect(file).not.toContain('\0')
// Vitest browser
expect(file).not.toContain('\x00')
}
})
test('multi environment coverage is merged correctly', async () => {
const coverageJson = await readCoverageJson()
const coverageMap = libCoverage.createCoverageMap(coverageJson as any)
const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/src/multi-environment.ts')
const lineCoverage = fileCoverage.getLineCoverage()
// Condition not covered by any test
expect(lineCoverage[13]).toBe(0)
// Condition covered by SSR test but not by Web
expect(lineCoverage[18]).toBe(1)
// Condition not covered by any test
expect(lineCoverage[22]).toBe(0)
// Condition covered by Web test but not by SSR
expect(lineCoverage[26]).toBe(1)
// Condition covered by both tests
expect(lineCoverage[30]).toBe(2)
})