Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(coverage): add allowExternal option #3894

Merged
merged 9 commits into from Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/config/index.md
Expand Up @@ -980,6 +980,15 @@ Since Vitest 0.31.0, you can check your coverage report in Vitest UI: check [Vit

Generate coverage report even when tests fail.

#### coverage.allowExternal

- **Type:** `boolean`
- **Default:** `false`
- **Available for providers:** `'v8' | 'istanbul'`
- **CLI:** `--coverage.allowExternal`, `--coverage.allowExternal=false`

Collect coverage of files outside the [project `root`](https://vitest.dev/config/#root).

#### coverage.skipFull

- **Type:** `boolean`
Expand Down
2 changes: 2 additions & 0 deletions packages/coverage-istanbul/src/provider.ts
Expand Up @@ -23,6 +23,7 @@ interface TestExclude {
exclude?: string | string[]
extension?: string | string[]
excludeNodeModules?: boolean
relativePath?: boolean
}): {
shouldInstrument(filePath: string): boolean
glob(cwd: string): Promise<string[]>
Expand Down Expand Up @@ -79,6 +80,7 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
exclude: [...defaultExclude, ...defaultInclude, ...this.options.exclude],
excludeNodeModules: true,
extension: this.options.extension,
relativePath: !this.options.allowExternal,
})
}

Expand Down
2 changes: 2 additions & 0 deletions packages/coverage-v8/src/provider.ts
Expand Up @@ -30,6 +30,7 @@ interface TestExclude {
exclude?: string | string[]
extension?: string | string[]
excludeNodeModules?: boolean
relativePath?: boolean
}): {
shouldInstrument(filePath: string): boolean
glob(cwd: string): Promise<string[]>
Expand Down Expand Up @@ -79,6 +80,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
exclude: [...defaultExclude, ...defaultInclude, ...this.options.exclude],
excludeNodeModules: true,
extension: this.options.extension,
relativePath: !this.options.allowExternal,
})
}

Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/defaults.ts
Expand Up @@ -38,6 +38,7 @@ export const coverageConfigDefaults: ResolvedCoverageOptions = {
reportOnFailure: false,
reporter: [['text', {}], ['html', {}], ['clover', {}], ['json', {}]],
extension: ['.js', '.cjs', '.mjs', '.ts', '.mts', '.cts', '.tsx', '.jsx', '.vue', '.svelte'],
allowExternal: false,
}

export const fakeTimersDefaults = {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/config.ts
Expand Up @@ -273,7 +273,7 @@ export function resolveConfig(
?? resolve(resolved.root, file),
),
)
resolved.coverage.exclude.push(...resolved.setupFiles.map(file => relative(resolved.root, file)))
resolved.coverage.exclude.push(...resolved.setupFiles.map(file => `${resolved.coverage.allowExternal ? '**/' : ''}${relative(resolved.root, file)}`))
AriPerkkio marked this conversation as resolved.
Show resolved Hide resolved

resolved.forceRerunTriggers = [
...resolved.forceRerunTriggers,
Expand Down
8 changes: 8 additions & 0 deletions packages/vitest/src/types/coverage.ts
Expand Up @@ -78,6 +78,7 @@ type FieldsWithDefaultValues =
| 'exclude'
| 'extension'
| 'reportOnFailure'
| 'allowExternal'

export type ResolvedCoverageOptions<T extends Provider = Provider> =
& CoverageOptions<T>
Expand Down Expand Up @@ -216,6 +217,13 @@ export interface BaseCoverageOptions {
* @default false
*/
reportOnFailure?: boolean

/**
* Collect coverage of files outside the project `root`.
*
* @default false
*/
allowExternal?: boolean
}

export interface CoverageIstanbulOptions extends BaseCoverageOptions {
Expand Down
Expand Up @@ -2,6 +2,89 @@

exports[`istanbul json report 1`] = `
{
"<process-cwd>/../test-utils/fixtures/math.ts": {
"b": {},
"branchMap": {},
"f": {
"0": 1,
"1": 0,
},
"fnMap": {
"0": {
"decl": {
"end": {
"column": 20,
"line": 1,
},
"start": {
"column": 16,
"line": 1,
},
},
"loc": {
"end": {
"column": null,
"line": 3,
},
"start": {
"column": 42,
"line": 1,
},
},
"name": "sum",
},
"1": {
"decl": {
"end": {
"column": 25,
"line": 5,
},
"start": {
"column": 16,
"line": 5,
},
},
"loc": {
"end": {
"column": null,
"line": 7,
},
"start": {
"column": 47,
"line": 5,
},
},
"name": "multiply",
},
},
"path": "<process-cwd>/../test-utils/fixtures/math.ts",
"s": {
"0": 1,
"1": 0,
},
"statementMap": {
"0": {
"end": {
"column": null,
"line": 2,
},
"start": {
"column": 2,
"line": 2,
},
},
"1": {
"end": {
"column": null,
"line": 6,
},
"start": {
"column": 2,
"line": 6,
},
},
},
},
"<process-cwd>/src/Counter/Counter.component.ts": {
"b": {},
"branchMap": {},
Expand Down
Expand Up @@ -2,6 +2,178 @@

exports[`v8 json report 1`] = `
{
"<process-cwd>/../test-utils/fixtures/math.ts": {
"all": false,
"b": {
"0": [
1,
],
},
"branchMap": {
"0": {
"line": 1,
"loc": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
"locations": [
{
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
],
"type": "branch",
},
},
"f": {
"0": 1,
"1": 0,
},
"fnMap": {
"0": {
"decl": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
"line": 1,
"loc": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
"name": "sum",
},
"1": {
"decl": {
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 7,
"line": 5,
},
},
"line": 5,
"loc": {
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 7,
"line": 5,
},
},
"name": "multiply",
},
},
"path": "<process-cwd>/../test-utils/fixtures/math.ts",
"s": {
"0": 1,
"1": 1,
"2": 1,
"3": 1,
"4": 1,
"5": 0,
"6": 0,
},
"statementMap": {
"0": {
"end": {
"column": 43,
"line": 1,
},
"start": {
"column": 0,
"line": 1,
},
},
"1": {
"end": {
"column": 14,
"line": 2,
},
"start": {
"column": 0,
"line": 2,
},
},
"2": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 0,
"line": 3,
},
},
"3": {
"end": {
"column": 0,
"line": 4,
},
"start": {
"column": 0,
"line": 4,
},
},
"4": {
"end": {
"column": 48,
"line": 5,
},
"start": {
"column": 0,
"line": 5,
},
},
"5": {
"end": {
"column": 14,
"line": 6,
},
"start": {
"column": 0,
"line": 6,
},
},
"6": {
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 0,
"line": 7,
},
},
},
},
"<process-cwd>/src/Counter/Counter.component.ts": {
"all": false,
"b": {
Expand Down