/
provider.ts
173 lines (142 loc) · 5.14 KB
/
provider.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { existsSync, promises as fs } from 'fs'
import _url from 'url'
import type { Profiler } from 'inspector'
import { takeCoverage } from 'v8'
import { extname, resolve } from 'pathe'
import type { RawSourceMap } from 'vite-node'
import { configDefaults } from 'vitest/config'
// eslint-disable-next-line no-restricted-imports
import type { CoverageC8Options, CoverageProvider, ReportContext, ResolvedCoverageOptions } from 'vitest'
import type { Vitest } from 'vitest/node'
// @ts-expect-error missing types
import createReport from 'c8/lib/report.js'
// @ts-expect-error missing types
import { checkCoverages } from 'c8/lib/commands/check-coverage.js'
export class C8CoverageProvider implements CoverageProvider {
name = 'c8'
ctx!: Vitest
options!: ResolvedCoverageOptions & { provider: 'c8' }
initialize(ctx: Vitest) {
this.ctx = ctx
this.options = resolveC8Options(ctx.config.coverage, ctx.config.root)
}
resolveOptions() {
return this.options
}
onBeforeFilesRun() {
process.env.NODE_V8_COVERAGE ||= this.options.tempDirectory
}
async clean(clean = true) {
if (clean && existsSync(this.options.reportsDirectory))
await fs.rm(this.options.reportsDirectory, { recursive: true, force: true, maxRetries: 10 })
if (!existsSync(this.options.tempDirectory))
await fs.mkdir(this.options.tempDirectory, { recursive: true })
}
onAfterSuiteRun() {
takeCoverage()
}
async reportCoverage({ allTestsRun }: ReportContext) {
takeCoverage()
const options = {
...this.options,
all: this.options.all && allTestsRun,
}
const report = createReport(options)
interface MapAndSource { map: RawSourceMap; source: string | undefined }
type SourceMapMeta = { url: string; filepath: string } & MapAndSource
// add source maps
const sourceMapMeta: Record<SourceMapMeta['url'], MapAndSource> = {}
const extensions = Array.isArray(this.options.extension) ? this.options.extension : [this.options.extension]
const entries = Array
.from(this.ctx.vitenode.fetchCache.entries())
.filter(i => !i[0].includes('/node_modules/'))
.map(([file, { result }]) => {
if (!result.map)
return null
const filepath = file.split('?')[0]
const url = _url.pathToFileURL(filepath).href
const extension = extname(file) || extname(url)
return {
filepath,
url,
extension,
map: result.map,
source: result.code,
}
})
.filter((entry) => {
if (!entry)
return false
if (!extensions.includes(entry.extension))
return false
// Mappings and sourcesContent are needed for C8 to work
return (
entry.map.mappings.length > 0
&& entry.map.sourcesContent
&& entry.map.sourcesContent.length > 0
&& entry.map.sourcesContent[0]
&& entry.map.sourcesContent[0].length > 0
)
}) as SourceMapMeta[]
await Promise.all(entries.map(async ({ url, source, map, filepath }) => {
if (url in sourceMapMeta)
return
let code: string | undefined
try {
code = (await fs.readFile(filepath)).toString()
}
catch { }
// Vite does not report full path in sourcemap sources
// so use an actual file path
const sources = [url]
sourceMapMeta[url] = {
source,
map: {
sourcesContent: code ? [code] : undefined,
...map,
sources,
},
}
}))
// This is a magic number. It corresponds to the amount of code
// that we add in packages/vite-node/src/client.ts:114 (vm.runInThisContext)
// TODO: Include our transformations in sourcemaps
const offset = 203
report._getSourceMap = (coverage: Profiler.ScriptCoverage) => {
const path = _url.pathToFileURL(coverage.url.split('?')[0]).href
const data = sourceMapMeta[path]
if (!data)
return {}
return {
sourceMap: {
sourcemap: data.map,
},
source: Array(offset).fill('.').join('') + data.source,
}
}
await report.run()
await checkCoverages(options, report)
// Note that this will only clean up the V8 reports generated so far.
// There will still be a temp directory with some reports when vitest exists,
// but at least it will only contain reports of vitest's internal functions.
if (existsSync(this.options.tempDirectory))
await fs.rm(this.options.tempDirectory, { recursive: true, force: true, maxRetries: 10 })
}
}
function resolveC8Options(options: CoverageC8Options, root: string) {
const resolved = {
...configDefaults.coverage,
...options as any,
}
if (options['100']) {
resolved.lines = 100
resolved.functions = 100
resolved.branches = 100
resolved.statements = 100
}
resolved.reporter = resolved.reporter || []
resolved.reporter = Array.isArray(resolved.reporter) ? resolved.reporter : [resolved.reporter]
resolved.reportsDirectory = resolve(root, resolved.reportsDirectory)
resolved.tempDirectory = process.env.NODE_V8_COVERAGE || resolve(resolved.reportsDirectory, 'tmp')
return resolved
}