-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@angular/build): support Istanbul coverage in Vitest runner #33029
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
Open
clydin
wants to merge
1
commit into
angular:main
Choose a base branch
from
clydin:feat/vitest-istanbul-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,74 @@ async function findTestEnvironment( | |
| } | ||
| } | ||
|
|
||
| function determineCoverageProvider( | ||
| browser: BrowserConfigOptions | undefined, | ||
| testConfig: InlineConfig | undefined, | ||
| optionsCoverageEnabled: boolean | undefined, | ||
| projectSourceRoot: string, | ||
| ): 'istanbul' | 'v8' | 'custom' | undefined { | ||
| let determinedProvider = testConfig?.coverage?.provider; | ||
| if (!determinedProvider && (optionsCoverageEnabled || testConfig?.coverage?.enabled)) { | ||
| const browsersToCheck = getBrowsersToCheck(browser, testConfig?.browser); | ||
|
|
||
| const hasNonChromium = | ||
| browsersToCheck | ||
| .map((b) => normalizeBrowserName(b).browser) | ||
| .filter((b) => !['chrome', 'chromium', 'edge'].includes(b)).length > 0; | ||
|
|
||
| if (hasNonChromium) { | ||
| determinedProvider = 'istanbul'; | ||
| } else { | ||
| const projectRequire = createRequire(projectSourceRoot + '/'); | ||
| const checkInstalled = (pkg: string) => { | ||
| try { | ||
| projectRequire.resolve(pkg); | ||
|
|
||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; | ||
| const hasIstanbul = checkInstalled('@vitest/coverage-istanbul'); | ||
| const hasV8 = checkInstalled('@vitest/coverage-v8'); | ||
|
|
||
| if (hasIstanbul && !hasV8) { | ||
| determinedProvider = 'istanbul'; | ||
| } else { | ||
| determinedProvider = 'v8'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return determinedProvider; | ||
| } | ||
|
|
||
| function getBrowsersToCheck( | ||
| browser: BrowserConfigOptions | undefined, | ||
| testConfigBrowser: BrowserConfigOptions | undefined, | ||
| ): string[] { | ||
| const browsersToCheck: string[] = []; | ||
|
|
||
| // 1. Check browsers passed by the Angular CLI options | ||
| const cliBrowser = browser as CustomBrowserConfigOptions | undefined; | ||
| if (cliBrowser?.instances) { | ||
| browsersToCheck.push(...cliBrowser.instances.map((i) => i.browser)); | ||
| } | ||
|
|
||
| // 2. Check browsers defined in the user's vitest.config.ts | ||
| const userBrowser = testConfigBrowser as CustomBrowserConfigOptions | undefined; | ||
| if (userBrowser) { | ||
| if (userBrowser.instances) { | ||
| browsersToCheck.push(...userBrowser.instances.map((i) => i.browser)); | ||
| } | ||
| if (userBrowser.name) { | ||
| browsersToCheck.push(userBrowser.name); | ||
| } | ||
| } | ||
|
|
||
| return browsersToCheck; | ||
| } | ||
|
Comment on lines
+114
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for collecting browsers to check has several issues:
The suggested implementation correctly prioritizes the CLI override and respects the 'enabled' flag while maintaining type safety for configuration properties. function getBrowsersToCheck(
browser: BrowserConfigOptions | undefined,
testConfigBrowser: BrowserConfigOptions | undefined,
): string[] {
const activeConfig = (browser ?? (testConfigBrowser?.enabled ? testConfigBrowser : undefined)) as
| CustomBrowserConfigOptions
| undefined;
if (!activeConfig) {
return [];
}
const browsersToCheck: string[] = [];
if (activeConfig.instances) {
browsersToCheck.push(...activeConfig.instances.map((i) => i.browser));
}
if (activeConfig.name) {
browsersToCheck.push(activeConfig.name);
}
return browsersToCheck;
}References
|
||
|
|
||
| export async function createVitestConfigPlugin( | ||
| options: VitestConfigPluginOptions, | ||
| ): Promise<VitestPlugins[0]> { | ||
|
|
@@ -89,6 +157,13 @@ export async function createVitestConfigPlugin( | |
| async config(config) { | ||
| const testConfig = config.test; | ||
|
|
||
| const determinedProvider = determineCoverageProvider( | ||
| browser, | ||
| testConfig, | ||
| options.coverage.enabled, | ||
| projectSourceRoot, | ||
| ); | ||
|
|
||
| if (reporters !== undefined) { | ||
| delete testConfig?.reporters; | ||
| } | ||
|
|
@@ -155,8 +230,8 @@ export async function createVitestConfigPlugin( | |
| (browser || testConfig?.browser?.enabled) && | ||
| (options.coverage.enabled || testConfig?.coverage?.enabled) | ||
| ) { | ||
| // Validate that enabled browsers support V8 coverage | ||
| validateBrowserCoverage(browser, testConfig?.browser); | ||
| // Validate that enabled browsers support the selected coverage provider | ||
| validateBrowserCoverage(browser, testConfig?.browser, determinedProvider); | ||
|
|
||
| projectPlugins.unshift(createSourcemapSupportPlugin()); | ||
| setupFiles.unshift('virtual:source-map-support'); | ||
|
|
@@ -208,6 +283,7 @@ export async function createVitestConfigPlugin( | |
| options.coverage, | ||
| testConfig?.coverage, | ||
| projectName, | ||
| determinedProvider, | ||
| ), | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| ...(reporters ? ({ reporters } as any) : {}), | ||
|
|
@@ -434,25 +510,12 @@ interface CustomBrowserConfigOptions { | |
| function validateBrowserCoverage( | ||
| browser: BrowserConfigOptions | undefined, | ||
| testConfigBrowser: BrowserConfigOptions | undefined, | ||
| provider?: string, | ||
| ): void { | ||
| const browsersToCheck: string[] = []; | ||
|
|
||
| // 1. Check browsers passed by the Angular CLI options | ||
| const cliBrowser = browser as CustomBrowserConfigOptions | undefined; | ||
| if (cliBrowser?.instances) { | ||
| browsersToCheck.push(...cliBrowser.instances.map((i) => i.browser)); | ||
| } | ||
|
|
||
| // 2. Check browsers defined in the user's vitest.config.ts | ||
| const userBrowser = testConfigBrowser as CustomBrowserConfigOptions | undefined; | ||
| if (userBrowser) { | ||
| if (userBrowser.instances) { | ||
| browsersToCheck.push(...userBrowser.instances.map((i) => i.browser)); | ||
| } | ||
| if (userBrowser.name) { | ||
| browsersToCheck.push(userBrowser.name); | ||
| } | ||
| if (provider === 'istanbul') { | ||
| return; | ||
| } | ||
| const browsersToCheck = getBrowsersToCheck(browser, testConfigBrowser); | ||
|
|
||
| // Normalize and filter unsupported browsers | ||
| const unsupportedBrowsers = browsersToCheck | ||
|
|
@@ -473,6 +536,7 @@ async function generateCoverageOption( | |
| optionsCoverage: NormalizedUnitTestBuilderOptions['coverage'], | ||
| configCoverage: VitestCoverageOption | undefined, | ||
| projectName: string, | ||
| provider?: 'istanbul' | 'v8' | 'custom', | ||
| ): Promise<VitestCoverageOption> { | ||
| let defaultExcludes: string[] = []; | ||
| // When a coverage exclude option is provided, Vitest's default coverage excludes | ||
|
|
@@ -486,6 +550,7 @@ async function generateCoverageOption( | |
| } | ||
|
|
||
| return { | ||
| provider, | ||
| excludeAfterRemap: true, | ||
| reportsDirectory: | ||
| configCoverage?.reportsDirectory ?? toPosixPath(path.join('coverage', projectName)), | ||
|
|
||
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,36 @@ | ||
| import { ng } from '../../utils/process'; | ||
| import { applyVitestBuilder } from '../../utils/vitest'; | ||
| import assert from 'node:assert'; | ||
| import { installPackage } from '../../utils/packages'; | ||
| import { expectFileToExist, readFile } from '../../utils/fs'; | ||
| import { updateJsonFile } from '../../utils/project'; | ||
|
|
||
| export default async function (): Promise<void> { | ||
| await applyVitestBuilder(); | ||
|
|
||
| // Install ONLY Istanbul coverage package. | ||
| // This will trigger the auto-detection logic to use Istanbul even for Node tests. | ||
| await installPackage('@vitest/coverage-istanbul@4'); | ||
|
|
||
| // Use the 'json' reporter to get a machine-readable output for assertions. | ||
| await updateJsonFile('angular.json', (json) => { | ||
| const project = Object.values(json['projects'])[0] as any; | ||
| const test = project['architect']['test']; | ||
| test.options = { | ||
| coverageReporters: ['json', 'text'], | ||
| }; | ||
| }); | ||
|
|
||
| // Run tests with coverage (defaults to Node/jsdom environment) | ||
| const { stdout } = await ng('test', '--no-watch', '--coverage'); | ||
|
|
||
| // Verify that tests passed | ||
| assert.match(stdout, /1 passed/, 'Expected tests to run successfully.'); | ||
|
|
||
| // Verify that coverage files are generated | ||
| const coverageJsonPath = 'coverage/test-project/coverage-final.json'; | ||
| await expectFileToExist(coverageJsonPath); | ||
|
|
||
| const coverageSummary = JSON.parse(await readFile(coverageJsonPath)); | ||
| assert.ok(Object.keys(coverageSummary).length > 0, 'Expected coverage report to not be empty.'); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using '.some()' is more efficient and readable than '.filter().length > 0' as it short-circuits as soon as a match is found. Additionally, performing the normalization inside the predicate avoids creating an intermediate array.