| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5194b41 commit 663c580
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,7 +22,7 @@ import { | |||
| 22 | 22 | } from '../utility/dependency'; | |
| 23 | 23 | import { JSONFile } from '../utility/json-file'; | |
| 24 | 24 | import { latestVersions } from '../utility/latest-versions'; | |
| 25 | - import { getWorkspace } from '../utility/workspace'; | ||
| 25 | + import { getWorkspace, updateWorkspace } from '../utility/workspace'; | ||
| 26 | 26 | import { Builders } from '../utility/workspace-models'; | |
| 27 | 27 | import { Schema as VitestBrowserOptions } from './schema'; | |
| 28 | 28 | ||
@@ -89,8 +89,19 @@ export default function (options: VitestBrowserOptions): Rule { | |||
| 89 | 89 | } | |
| 90 | 90 | }; | |
| 91 | 91 | ||
| 92 | + // Update angular.json to add the browsers option to the test target | ||
| 93 | + const defaultBrowser = packageName === '@vitest/browser-webdriverio' ? 'chrome' : 'chromium'; | ||
| 94 | + | ||
| 92 | 95 | return chain([ | |
| 93 | 96 | updateTsConfigRule, | |
| 97 | + updateWorkspace((workspace) => { | ||
| 98 | + const testTarget = workspace.projects.get(options.project)?.targets.get('test'); | ||
| 99 | + | ||
| 100 | + if (testTarget) { | ||
| 101 | + testTarget.options ??= {}; | ||
| 102 | + testTarget.options['browsers'] ??= [defaultBrowser]; | ||
| 103 | + } | ||
| 104 | + }), | ||
| 94 | 105 | ...dependencies.map((name) => | |
| 95 | 106 | addDependency(name, latestVersions[name], { | |
| 96 | 107 | type: DependencyType.Dev, | |
@@ -101,8 +112,7 @@ export default function (options: VitestBrowserOptions): Rule { | |||
| 101 | 112 | (_, context) => { | |
| 102 | 113 | context.logger.info( | |
| 103 | 114 | '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.", | ||
| 115 | + `The test target has been configured with '${defaultBrowser}' as browser.`, | ||
| 106 | 116 | ); | |
| 107 | 117 | }, | |
| 108 | 118 | ]); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,37 +35,69 @@ describe('Vitest Browser Provider Schematic', () => { | |||
| 35 | 35 | ); | |
| 36 | 36 | }); | |
| 37 | 37 | ||
| 38 | - it('should add dependencies and update tsconfig.spec.json', async () => { | ||
| 39 | - const options = { | ||
| 40 | - project: 'app', | ||
| 41 | - package: '@vitest/browser-playwright', | ||
| 42 | - skipInstall: true, | ||
| 43 | - }; | ||
| 38 | + [ | ||
| 39 | + { pkg: '@vitest/browser-playwright', browser: 'chromium', extraDeps: ['playwright'] }, | ||
| 40 | + { pkg: '@vitest/browser-webdriverio', browser: 'chrome', extraDeps: ['webdriverio'] }, | ||
| 41 | + { pkg: '@vitest/browser-preview', browser: 'chromium', extraDeps: [] as string[] }, | ||
| 42 | + ].forEach(({ pkg, browser, extraDeps }) => { | ||
| 43 | + it(`should add ${browser} to browsers option in angular.json for ${pkg}`, async () => { | ||
| 44 | + const options = { | ||
| 45 | + project: 'app', | ||
| 46 | + package: pkg, | ||
| 47 | + skipInstall: true, | ||
| 48 | + }; | ||
| 44 | 49 | ||
| 45 | - const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree); | ||
| 50 | + const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree); | ||
| 46 | 51 | ||
| 47 | - const packageJson = parse(resultTree.readContent('/package.json')); | ||
| 48 | - expect(packageJson.devDependencies['@vitest/browser-playwright']).toBeDefined(); | ||
| 49 | - expect(packageJson.devDependencies['playwright']).toBeDefined(); | ||
| 52 | + const angularJson = parse(resultTree.readContent('/angular.json')); | ||
| 53 | + const project = angularJson.projects.app; | ||
| 54 | + const targets = project.architect || project.targets; | ||
| 55 | + expect(targets.test.options.browsers).toEqual([browser]); | ||
| 56 | + }); | ||
| 50 | 57 | ||
| 51 | - const tsConfig = parse(resultTree.readContent('/projects/app/tsconfig.spec.json')); | ||
| 52 | - expect(tsConfig.compilerOptions.types).toContain('vitest/globals'); | ||
| 53 | - expect(tsConfig.compilerOptions.types).toContain('@vitest/browser-playwright'); | ||
| 54 | - expect(tsConfig.compilerOptions.types).not.toContain('jasmine'); | ||
| 58 | + it(`should add dependencies and update tsconfig.spec.json for ${pkg}`, async () => { | ||
| 59 | + const options = { | ||
| 60 | + project: 'app', | ||
| 61 | + package: pkg, | ||
| 62 | + skipInstall: true, | ||
| 63 | + }; | ||
| 64 | + | ||
| 65 | + const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree); | ||
| 66 | + | ||
| 67 | + const packageJson = parse(resultTree.readContent('/package.json')); | ||
| 68 | + expect(packageJson.devDependencies[pkg]).toBeDefined(); | ||
| 69 | + for (const dep of extraDeps) { | ||
| 70 | + expect(packageJson.devDependencies[dep]).toBeDefined(); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + const tsConfig = parse(resultTree.readContent('/projects/app/tsconfig.spec.json')); | ||
| 74 | + expect(tsConfig.compilerOptions.types).toContain('vitest/globals'); | ||
| 75 | + expect(tsConfig.compilerOptions.types).toContain(pkg); | ||
| 76 | + expect(tsConfig.compilerOptions.types).not.toContain('jasmine'); | ||
| 77 | + }); | ||
| 55 | 78 | }); | |
| 56 | 79 | ||
| 57 | - it('should add webdriverio dependency when @vitest/browser-webdriverio is used', async () => { | ||
| 80 | + it('should not overwrite existing browsers option in angular.json', async () => { | ||
| 81 | + // Set up existing browsers option | ||
| 82 | + const angularJson = parse(tree.readContent('/angular.json')); | ||
| 83 | + const project = angularJson.projects.app; | ||
| 84 | + const targets = project.architect || project.targets; | ||
| 85 | + targets.test.options ??= {}; | ||
| 86 | + targets.test.options.browsers = ['firefox']; | ||
| 87 | + tree.overwrite('/angular.json', JSON.stringify(angularJson)); | ||
| 88 | + | ||
| 58 | 89 | const options = { | |
| 59 | 90 | project: 'app', | |
| 60 | - package: '@vitest/browser-webdriverio', | ||
| 91 | + package: '@vitest/browser-playwright', | ||
| 61 | 92 | skipInstall: true, | |
| 62 | 93 | }; | |
| 63 | 94 | ||
| 64 | 95 | const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree); | |
| 65 | 96 | ||
| 66 | - const packageJson = parse(resultTree.readContent('/package.json')); | ||
| 67 | - expect(packageJson.devDependencies['@vitest/browser-webdriverio']).toBeDefined(); | ||
| 68 | - expect(packageJson.devDependencies['webdriverio']).toBeDefined(); | ||
| 97 | + const updatedAngularJson = parse(resultTree.readContent('/angular.json')); | ||
| 98 | + const updatedProject = updatedAngularJson.projects.app; | ||
| 99 | + const updatedTargets = updatedProject.architect || updatedProject.targets; | ||
| 100 | + expect(updatedTargets.test.options.browsers).toEqual(['firefox']); | ||
| 69 | 101 | }); | |
| 70 | 102 | ||
| 71 | 103 | it('should update tsconfig.spec.json for a library project', async () => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments