MCPcopy
hub / github.com/vitest-dev/vitest / getSources

Method getSources

packages/coverage-v8/src/provider.ts:332–377  ·  view source on GitHub ↗
(
    url: string,
    onTransform: (filepath: string) => Promise<Vite.TransformResult | undefined | null>,
    functions: Profiler.FunctionCoverage[] = [],
  )

Source from the content-addressed store, hash-verified

330 }
331
332 private async getSources(
333 url: string,
334 onTransform: (filepath: string) => Promise<Vite.TransformResult | undefined | null>,
335 functions: Profiler.FunctionCoverage[] = [],
336 ): Promise<{
337 code: string
338 map?: Vite.Rollup.SourceMap
339 }> {
340 // TODO: need to standardize file urls before this call somehow, this is messy
341 const filepath = url.match(/^file:\/\/\/\w:\//)
342 ? url.slice(8)
343 : removeStartsWith(url, FILE_PROTOCOL)
344 // TODO: do we still need to "catch" here? why would it fail?
345 const transformResult = await onTransform(filepath).catch(() => null)
346
347 const map = transformResult?.map as Vite.Rollup.SourceMap | undefined
348 const code = transformResult?.code
349
350 if (code == null) {
351 const filePath = normalize(fileURLToPath(url))
352
353 const original = await fs.readFile(filePath, 'utf-8').catch(() => {
354 // If file does not exist construct a dummy source for it.
355 // These can be files that were generated dynamically during the test run and were removed after it.
356 const length = findLongestFunctionLength(functions)
357 return '/'.repeat(length)
358 })
359
360 return { code: original }
361 }
362
363 // Vue needs special handling for "map.sources"
364 if (map) {
365 map.sources ||= []
366
367 map.sources = map.sources
368 .filter(source => source != null)
369 .map(source => new URL(source, url).href)
370
371 if (map.sources.length === 0) {
372 map.sources.push(url)
373 }
374 }
375
376 return { code, map }
377 }
378
379 private async convertCoverage(
380 coverage: RawCoverage,

Callers 2

convertCoverageMethod · 0.95

Calls 6

removeStartsWithFunction · 0.85
normalizeFunction · 0.85
readFileMethod · 0.80
filterMethod · 0.65
matchMethod · 0.45

Tested by

no test coverage detected