fix(@schematics/angular): add browsers option during vitest browser provider ng-add#32865
fix(@schematics/angular): add browsers option during vitest browser provider ng-add#32865maruthang wants to merge 1 commit intoangular:mainfrom
Conversation
…vitest browser provider ng-add When adding a Vitest browser provider via ng-add, the schematic now automatically configures the 'browsers' option in angular.json with an appropriate default browser (chromium for Playwright/Preview, chrome for WebDriverIO) instead of only logging a manual instruction. Fixes angular#32401
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request updates the vitest-browser schematic to automatically configure the browsers option in angular.json based on the selected provider package. It defaults to 'chrome' for WebdriverIO and 'chromium' for Playwright or Preview, while ensuring existing configurations are not overwritten. Unit tests have been added to verify these changes. The review feedback suggests refactoring the browser selection logic for better clarity using a ternary operator and removing redundant conditional checks for project targets that are already validated upstream.
| let defaultBrowser: string; | ||
| if (packageName === '@vitest/browser-webdriverio') { | ||
| defaultBrowser = 'chrome'; | ||
| } else { | ||
| // Playwright and preview both use 'chromium' as the default | ||
| defaultBrowser = 'chromium'; | ||
| } |
There was a problem hiding this comment.
For better code clarity and to prevent accidental reassignment, you can declare defaultBrowser as a const and initialize it using a ternary operator.
// Playwright and preview both use 'chromium' as the default
const defaultBrowser = packageName === '@vitest/browser-webdriverio' ? 'chrome' : 'chromium';| const project = workspace.projects.get(options.project); | ||
| if (project) { | ||
| const testTarget = project.targets.get('test'); | ||
| if (testTarget) { | ||
| testTarget.options ??= {}; | ||
| const existingBrowsers = testTarget.options['browsers'] as string[] | undefined; | ||
| if (!existingBrowsers?.length) { | ||
| testTarget.options['browsers'] = [defaultBrowser]; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The nested if checks for project and testTarget are redundant because their existence is already validated at the start of the schematic (lines 31-43), where an exception is thrown if they are not found. You can simplify this code to improve readability, as explicit checks are redundant when upstream processes guarantee existence.
const project = workspace.projects.get(options.project);
const testTarget = project?.targets.get('test');
if (testTarget) {
testTarget.options ??= {};
const existingBrowsers = testTarget.options['browsers'] as string[] | undefined;
if (!existingBrowsers?.length) {
testTarget.options['browsers'] = [defaultBrowser];
}
}References
- When refactoring code that handles configuration properties, explicit checks can be removed if the property is guaranteed to be valid by an upstream process.
PR Checklist
PR Type
What is the current behavior?
When running ng-add for a Vitest browser provider (e.g.,
@vitest/browser-playwright), the schematic only logs a message telling users to manually add thebrowsersoption toangular.json. The configuration is incomplete.Issue Number: #32401
What is the new behavior?
The schematic now automatically configures the
browsersoption inangular.jsonwith an appropriate default browser:chromiumfor Playwright and Preview providerschromefor WebDriverIOExisting
browsersconfigurations are preserved and not overwritten.Does this PR introduce a breaking change?