Skip to content

fix(@schematics/angular): add browsers option during vitest browser provider ng-add#32865

Closed
maruthang wants to merge 1 commit intoangular:mainfrom
maruthang:fix-32401-vitest-browser-ng-add
Closed

fix(@schematics/angular): add browsers option during vitest browser provider ng-add#32865
maruthang wants to merge 1 commit intoangular:mainfrom
maruthang:fix-32401-vitest-browser-ng-add

Conversation

@maruthang
Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

  • Bugfix

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 the browsers option to angular.json. The configuration is incomplete.

Issue Number: #32401

What is the new behavior?

The schematic now automatically configures the browsers option in angular.json with an appropriate default browser:

  • chromium for Playwright and Preview providers
  • chrome for WebDriverIO

Existing browsers configurations are preserved and not overwritten.

Does this PR introduce a breaking change?

  • Yes
  • No

…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
@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 27, 2026

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.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +93 to +99
let defaultBrowser: string;
if (packageName === '@vitest/browser-webdriverio') {
defaultBrowser = 'chrome';
} else {
// Playwright and preview both use 'chromium' as the default
defaultBrowser = 'chromium';
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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';

Comment on lines +103 to +113
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];
}
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
  1. When refactoring code that handles configuration properties, explicit checks can be removed if the property is guaranteed to be valid by an upstream process.

@maruthang maruthang deleted the fix-32401-vitest-browser-ng-add branch March 27, 2026 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants