-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(api): add enableCoverage and disableCoverage methods
#8412
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
Merged
sheremet-va
merged 10 commits into
vitest-dev:main
from
sheremet-va:feat/expose-dynamic-coverage-enable
Aug 12, 2025
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5dca664
feat(api): add `enableCoverage` and `disableCoverage` methods
sheremet-va ce735d4
chore: cleanup
sheremet-va dc02198
docs: add coverage docs
sheremet-va 9cceab0
fix: properly disable/enable coverage
sheremet-va 35b093e
test: update tests
sheremet-va b06dd06
Merge branch 'main' of github.com:vitest-dev/vitest into feat/expose-…
sheremet-va 02619bc
fix: invalidate istanbul modules by default
sheremet-va a8b6903
chore: cleanup
sheremet-va 0fadae1
chore: remove unused default arg
sheremet-va 95a3940
chore: syntax error
sheremet-va File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
| import { expect } from 'vitest' | ||
| import { cleanupCoverageJson, readCoverageMap, runVitest, test } from '../utils' | ||
|
|
||
| test('enableCoverage() collects coverage after being called', async () => { | ||
| await cleanupCoverageJson() | ||
|
|
||
| // Run a minimal suite where coverage starts disabled, then enable it and rerun. | ||
| const { ctx } = await runVitest({ | ||
| include: ['fixtures/test/math.test.ts'], | ||
| coverage: { | ||
| // start disabled and turn on dynamically | ||
| enabled: false, | ||
| reporter: 'json', | ||
| include: [ | ||
| 'fixtures/src/math.ts', | ||
| 'fixtures/src/untested-file.ts', | ||
| ], | ||
sheremet-va marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| }) | ||
|
|
||
| await expect(readCoverageMap(), 'coverage map is not on the disk').rejects.toThrowError(/no such file/) | ||
sheremet-va marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| await ctx!.enableCoverage() | ||
| expect(ctx!.coverageProvider).toBeTruthy() | ||
|
|
||
| await ctx!.rerunFiles() | ||
|
|
||
| const coverageMap = await readCoverageMap() | ||
| expect(coverageMap.files()).toContain('<process-cwd>/fixtures/src/math.ts') | ||
| }) | ||
|
|
||
| test('disableCoverage() stops collecting coverage going forward', async () => { | ||
| const { ctx } = await runVitest({ | ||
| include: ['fixtures/test/math.test.ts'], | ||
| coverage: { | ||
| enabled: true, | ||
| reporter: 'json', | ||
| include: [ | ||
| 'fixtures/src/math.ts', | ||
| 'fixtures/src/untested-file.ts', | ||
| ], | ||
sheremet-va marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // try to clean on rerun when provider is enabled | ||
| cleanOnRerun: true, | ||
| }, | ||
| }) | ||
|
|
||
| // Initial run collects coverage | ||
| const initialMap = await readCoverageMap() | ||
| expect(initialMap.files()).toContain('<process-cwd>/fixtures/src/math.ts') | ||
| expect(ctx!.coverageProvider).toBeTruthy() | ||
|
|
||
| // Disable coverage and rerun | ||
| ctx!.disableCoverage() | ||
| expect(ctx!.coverageProvider).toBeNull() | ||
|
|
||
| await cleanupCoverageJson() | ||
|
|
||
| await ctx!.rerunFiles() | ||
|
|
||
| await expect(readCoverageMap(), 'coverage map is not on the disk').rejects.toThrowError(/no such file/) | ||
| expect(ctx!.coverageProvider).toBeNull() | ||
| }) | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.