-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathprovider.ts
194 lines (157 loc) · 5.68 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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 c from 'picocolors'
import { provider } from 'std-env'
import type { RawSourceMap } from 'vite-node'
import { coverageConfigDefaults } from 'vitest/config'
// eslint-disable-next-line no-restricted-imports
import type { CoverageC8Options, CoverageProvider, ReportContext, ResolvedCoverageOptions } from 'vitest'
import type { Vitest } from 'vitest/node'
import type { Report } from 'c8'
// @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'
type Options =
& ResolvedCoverageOptions<'c8'>
& { tempDirectory: string }
export class C8CoverageProvider implements CoverageProvider {
name = 'c8'
ctx!: Vitest
options!: Options
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()
if (provider === 'stackblitz')
this.ctx.logger.log(c.blue(' % ') + c.yellow('@vitest/coverage-c8 does not work on Stackblitz. Report will be empty.'))
const options: ConstructorParameters<typeof Report>[0] = {
...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 = 185
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): Options {
const reportsDirectory = resolve(root, options.reportsDirectory || coverageConfigDefaults.reportsDirectory)
const reporter = options.reporter || coverageConfigDefaults.reporter
const resolved: Options = {
...coverageConfigDefaults,
// Provider specific defaults
excludeNodeModules: true,
allowExternal: false,
// User's options
...options,
// Resolved fields
provider: 'c8',
tempDirectory: process.env.NODE_V8_COVERAGE || resolve(reportsDirectory, 'tmp'),
reporter: Array.isArray(reporter) ? reporter : [reporter],
reportsDirectory,
}
if (options['100']) {
resolved.lines = 100
resolved.functions = 100
resolved.branches = 100
resolved.statements = 100
}
return resolved
}