Skip to content

Commit

Permalink
fix(coverage): ignore generated TS decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Feb 19, 2024
1 parent d4bb531 commit 7df89f3
Show file tree
Hide file tree
Showing 12 changed files with 994 additions and 10 deletions.
3 changes: 3 additions & 0 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
const sourceMap = pluginCtx.getCombinedSourcemap()
sourceMap.sources = sourceMap.sources.map(removeQueryParameters)

// Exclude SWC's decorators that are left in source maps
sourceCode = sourceCode.replaceAll('_ts_decorate', '/* istanbul ignore next */_ts_decorate')

const code = this.instrumenter.instrumentSync(sourceCode, id, sourceMap as any)
const map = this.instrumenter.lastSourceMap() as any

Expand Down
24 changes: 14 additions & 10 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const WRAPPER_LENGTH = 185

// Note that this needs to match the line ending as well
const VITE_EXPORTS_LINE_PATTERN = /Object\.defineProperty\(__vite_ssr_exports__.*\n/g
const DECORATOR_METADATA_PATTERN = /_ts_metadata\("design:paramtypes"(\s|.)+?]\),/g
const DEFAULT_PROJECT = Symbol.for('default-project')

const debug = createDebug('vitest:coverage')
Expand Down Expand Up @@ -315,7 +316,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
originalSource: sourcesContent,
source: code || sourcesContent,
sourceMap: {
sourcemap: removeViteHelpersFromSourceMaps(code, {
sourcemap: excludeGeneratedCode(code, {
...map,
version: 3,
sources: [url],
Expand Down Expand Up @@ -366,21 +367,24 @@ async function transformCoverage(coverageMap: CoverageMap) {
/**
* Remove generated code from the source maps:
* - Vite's export helpers: e.g. `Object.defineProperty(__vite_ssr_exports__, "sum", { enumerable: true, configurable: true, get(){ return sum }});`
* - SWC's decorator metadata: e.g. `_ts_metadata("design:paramtypes", [\ntypeof Request === "undefined" ? Object : Request\n]),`
*/
function removeViteHelpersFromSourceMaps(source: string | undefined, map: EncodedSourceMap) {
if (!source || !source.match(VITE_EXPORTS_LINE_PATTERN))
function excludeGeneratedCode(source: string | undefined, map: EncodedSourceMap) {
if (!source)
return map

const sourceWithoutHelpers = new MagicString(source)
sourceWithoutHelpers.replaceAll(VITE_EXPORTS_LINE_PATTERN, '\n')
if (!source.match(VITE_EXPORTS_LINE_PATTERN) && !source.match(DECORATOR_METADATA_PATTERN))
return map

const trimmed = new MagicString(source)
trimmed.replaceAll(VITE_EXPORTS_LINE_PATTERN, '\n')
trimmed.replaceAll(DECORATOR_METADATA_PATTERN, match => '\n'.repeat(match.split('\n').length - 1))

const mapWithoutHelpers = sourceWithoutHelpers.generateMap({
hires: 'boundary',
})
const trimmedMap = trimmed.generateMap({ hires: 'boundary' })

// A merged source map where the first one excludes helpers
// A merged source map where the first one excludes generated parts
const combinedMap = remapping(
[{ ...mapWithoutHelpers, version: 3 }, map],
[{ ...trimmedMap, version: 3 }, map],
() => null,
)

Expand Down
172 changes: 172 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exports[`custom json report 1`] = `
"<process-cwd>/src/Defined.vue",
"<process-cwd>/src/Hello.vue",
"<process-cwd>/src/another-setup.ts",
"<process-cwd>/src/decorators.ts",
"<process-cwd>/src/dynamic-file-esm.ignore.js",
"<process-cwd>/src/dynamic-files.ts",
"<process-cwd>/src/function-count.ts",
Expand Down
Loading

0 comments on commit 7df89f3

Please sign in to comment.