-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
istanbul
coverage provider
- Loading branch information
1 parent
ef2aaf1
commit e657060
Showing
13 changed files
with
294 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { existsSync, promises as fs } from 'fs' | ||
import { createRequire } from 'module' | ||
import { resolve } from 'pathe' | ||
import type { ExistingRawSourceMap, TransformPluginContext } from 'rollup' | ||
|
||
import { configDefaults, defaultExclude, defaultInclude } from '../../defaults' | ||
import type { Vitest } from '../../node' | ||
import type { IstanbulOptions, ResolvedCoverageOptions } from '../../types' | ||
import type { BaseCoverageReporter } from './base' | ||
|
||
const require = createRequire(import.meta.url) | ||
const coverageVariable = '__VITEST_COVERAGE__' | ||
|
||
interface Instrumenter { | ||
/* Instrument the supplied code and track coverage against the supplied filename. It throws if invalid code is passed to it. ES5 and ES6 syntax is supported. To instrument ES6 modules, make sure that you set the esModules property to true when creating the instrumenter. */ | ||
instrumentSync( | ||
/* The code to instrument */ | ||
code: string, | ||
/* The filename against which to track coverage. */ | ||
filename: string, | ||
/* The source map that maps the not instrumented code back to it's original form. Is assigned to the coverage object and therefore, is available in the json output and can be used to remap the coverage to the untranspiled source.): string; */ | ||
inputSourceMap: object | ||
): string | ||
|
||
/* Returns the file coverage object for the last file instrumented. */ | ||
lastSourceMap(): ExistingRawSourceMap | ||
} | ||
|
||
interface TestExclude { | ||
new(opts: { | ||
cwd?: string | string[] | ||
include?: string | string[] | ||
exclude?: string | string[] | ||
extension?: string | string[] | ||
excludeNodeModules?: boolean | ||
}): { shouldInstrument(filePath: string): boolean } | ||
} | ||
|
||
export class IstanbulReporter implements BaseCoverageReporter { | ||
ctx!: Vitest | ||
options!: ResolvedCoverageOptions & { provider: 'istanbul' } | ||
instrumenter!: Instrumenter | ||
testExclude!: InstanceType<TestExclude> | ||
coverages: any[] = [] | ||
|
||
initialize(ctx: Vitest) { | ||
this.ctx = ctx | ||
this.options = resolveIstanbulOptions(ctx.config.coverage, ctx.config.root) | ||
|
||
const { createInstrumenter } = require('istanbul-lib-instrument') | ||
this.instrumenter = createInstrumenter(this.options) | ||
|
||
const TestExclude = require('test-exclude') | ||
|
||
this.testExclude = new TestExclude({ | ||
cwd: ctx.config.root, | ||
exclude: [...defaultExclude, ...defaultInclude], | ||
extension: ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue', '.svelte'], | ||
excludeNodeModules: true, | ||
}) | ||
} | ||
|
||
resolveOptions(): ResolvedCoverageOptions { | ||
return this.options | ||
} | ||
|
||
onFileTransform(sourceCode: string, id: string, pluginCtx: TransformPluginContext) { | ||
if (!this.testExclude.shouldInstrument(id)) | ||
return | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- ignoreRestSiblings should be enabled | ||
const { sourcesContent, ...sourceMap } = pluginCtx.getCombinedSourcemap() | ||
const code = this.instrumenter.instrumentSync(sourceCode, id, sourceMap) | ||
const map = this.instrumenter.lastSourceMap() | ||
|
||
return { code, map } | ||
} | ||
|
||
onAfterSuiteRun(coverage: any) { | ||
// TODO: Some implementations write these into file system instead of storing in memory. | ||
// Then when merging the results, JSONs are read & deleted from fs and convert into coverageMap | ||
this.coverages.push(coverage) | ||
} | ||
|
||
async clean(clean = true) { | ||
if (clean && existsSync(this.options.reportsDirectory)) | ||
await fs.rm(this.options.reportsDirectory, { recursive: true, force: true }) | ||
|
||
this.coverages = [] | ||
} | ||
|
||
async onAfterAllFilesRun() { | ||
const libReport = require('istanbul-lib-report') | ||
const reports = require('istanbul-reports') | ||
const libCoverage = require('istanbul-lib-coverage') | ||
const libSourceMaps = require('istanbul-lib-source-maps') | ||
|
||
const mergedCoverage = this.coverages.reduce((coverage, previousCoverageMap) => { | ||
const map = libCoverage.createCoverageMap(coverage) | ||
map.merge(previousCoverageMap) | ||
|
||
return map | ||
}, {}) | ||
|
||
const sourceMapStore = libSourceMaps.createSourceMapStore() | ||
const coverageMap = await sourceMapStore.transformCoverage(mergedCoverage) | ||
|
||
const context = libReport.createContext({ | ||
dir: this.options.reportsDirectory, | ||
coverageMap, | ||
sourceFinder: sourceMapStore.sourceFinder, | ||
}) | ||
|
||
for (const reporter of this.options.reporter) | ||
reports.create(reporter).execute(context) | ||
} | ||
} | ||
|
||
function resolveIstanbulOptions(options: IstanbulOptions, root: string) { | ||
const reportsDirectory = resolve(root, options.reportsDirectory || configDefaults.coverage.reportsDirectory!) | ||
|
||
const resolved = { | ||
...configDefaults.coverage, | ||
|
||
// Custom | ||
provider: 'istanbul', | ||
coverageVariable, | ||
coverageGlobalScope: 'globalThis', | ||
coverageGlobalScopeFunc: false, | ||
esModules: true, | ||
|
||
// Defaults from nyc, https://github.com/istanbuljs/nyc/blob/master/lib/instrumenters/istanbul.js#L7 | ||
preserveComments: true, | ||
produceSourceMap: true, | ||
autoWrap: true, | ||
|
||
// Overrides | ||
...options, | ||
|
||
reportsDirectory, | ||
tempDirectory: resolve(reportsDirectory, 'tmp'), | ||
} | ||
|
||
return resolved as ResolvedCoverageOptions & { provider: 'istanbul' } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Plugin as VitePlugin } from 'vite' | ||
|
||
import type { Vitest } from '../core' | ||
|
||
export function InstrumenterPlugin(ctx: Vitest): VitePlugin | null { | ||
// // Skip coverage reporters which do not need code transforms, e.g. native v8 | ||
// TODO: This would be great but ctx has not yet been initialized | ||
// if (typeof ctx.coverageReporter.onFileTransform !== 'function') | ||
// return null | ||
|
||
return { | ||
name: 'vitest:instrumenter', | ||
|
||
config(config) { | ||
if (config.test?.coverage && !config?.build?.sourcemap) { | ||
config.build = config.build || {} | ||
config.build.sourcemap = true | ||
} | ||
}, | ||
|
||
transform(srcCode, id) { | ||
return ctx.coverageReporter?.onFileTransform?.(srcCode, id, this) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.