Skip to content

Commit d7a1153

Browse files
author
maruthan
committed
fix(@schematics/angular): add browsers option to angular.json during 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 #32401
1 parent f1ed025 commit d7a1153

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

packages/schematics/angular/vitest-browser/index.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from '../utility/dependency';
2323
import { JSONFile } from '../utility/json-file';
2424
import { latestVersions } from '../utility/latest-versions';
25-
import { getWorkspace } from '../utility/workspace';
25+
import { getWorkspace, updateWorkspace } from '../utility/workspace';
2626
import { Builders } from '../utility/workspace-models';
2727
import { Schema as VitestBrowserOptions } from './schema';
2828

@@ -89,8 +89,33 @@ export default function (options: VitestBrowserOptions): Rule {
8989
}
9090
};
9191

92+
// Determine the default browser based on the provider package
93+
let defaultBrowser: string;
94+
if (packageName === '@vitest/browser-webdriverio') {
95+
defaultBrowser = 'chrome';
96+
} else {
97+
// Playwright and preview both use 'chromium' as the default
98+
defaultBrowser = 'chromium';
99+
}
100+
101+
// Update angular.json to add the browsers option to the test target
102+
const updateAngularJsonRule = updateWorkspace((workspace) => {
103+
const project = workspace.projects.get(options.project);
104+
if (project) {
105+
const testTarget = project.targets.get('test');
106+
if (testTarget) {
107+
testTarget.options ??= {};
108+
const existingBrowsers = testTarget.options['browsers'] as string[] | undefined;
109+
if (!existingBrowsers?.length) {
110+
testTarget.options['browsers'] = [defaultBrowser];
111+
}
112+
}
113+
}
114+
});
115+
92116
return chain([
93117
updateTsConfigRule,
118+
updateAngularJsonRule,
94119
...dependencies.map((name) =>
95120
addDependency(name, latestVersions[name], {
96121
type: DependencyType.Dev,
@@ -101,8 +126,7 @@ export default function (options: VitestBrowserOptions): Rule {
101126
(_, context) => {
102127
context.logger.info(
103128
'Vitest browser testing support has been added. ' +
104-
"To run tests in a browser, add a 'browsers' field to the 'test' target in 'angular.json', " +
105-
"or use the '--browsers' command line option.",
129+
`The test target has been configured with '${defaultBrowser}' as the default browser.`,
106130
);
107131
},
108132
]);

packages/schematics/angular/vitest-browser/index_spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,59 @@ describe('Vitest Browser Provider Schematic', () => {
5454
expect(tsConfig.compilerOptions.types).not.toContain('jasmine');
5555
});
5656

57+
it('should add browsers option to angular.json for playwright', async () => {
58+
const options = {
59+
project: 'app',
60+
package: '@vitest/browser-playwright',
61+
skipInstall: true,
62+
};
63+
64+
const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree);
65+
66+
const angularJson = parse(resultTree.readContent('/angular.json'));
67+
const project = angularJson.projects.app;
68+
const targets = project.architect || project.targets;
69+
expect(targets.test.options.browsers).toEqual(['chromium']);
70+
});
71+
72+
it('should add browsers option to angular.json for webdriverio', async () => {
73+
const options = {
74+
project: 'app',
75+
package: '@vitest/browser-webdriverio',
76+
skipInstall: true,
77+
};
78+
79+
const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree);
80+
81+
const angularJson = parse(resultTree.readContent('/angular.json'));
82+
const project = angularJson.projects.app;
83+
const targets = project.architect || project.targets;
84+
expect(targets.test.options.browsers).toEqual(['chrome']);
85+
});
86+
87+
it('should not overwrite existing browsers option in angular.json', async () => {
88+
// Set up existing browsers option
89+
const angularJson = parse(tree.readContent('/angular.json'));
90+
const project = angularJson.projects.app;
91+
const targets = project.architect || project.targets;
92+
targets.test.options ??= {};
93+
targets.test.options.browsers = ['firefox'];
94+
tree.overwrite('/angular.json', JSON.stringify(angularJson));
95+
96+
const options = {
97+
project: 'app',
98+
package: '@vitest/browser-playwright',
99+
skipInstall: true,
100+
};
101+
102+
const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree);
103+
104+
const updatedAngularJson = parse(resultTree.readContent('/angular.json'));
105+
const updatedProject = updatedAngularJson.projects.app;
106+
const updatedTargets = updatedProject.architect || updatedProject.targets;
107+
expect(updatedTargets.test.options.browsers).toEqual(['firefox']);
108+
});
109+
57110
it('should add webdriverio dependency when @vitest/browser-webdriverio is used', async () => {
58111
const options = {
59112
project: 'app',

0 commit comments

Comments
 (0)