|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + Rule, |
| 11 | + SchematicContext, |
| 12 | + SchematicsException, |
| 13 | + Tree, |
| 14 | + chain, |
| 15 | +} from '@angular-devkit/schematics'; |
| 16 | +import { join } from 'node:path/posix'; |
| 17 | +import { |
| 18 | + DependencyType, |
| 19 | + ExistingBehavior, |
| 20 | + InstallBehavior, |
| 21 | + addDependency, |
| 22 | +} from '../utility/dependency'; |
| 23 | +import { JSONFile } from '../utility/json-file'; |
| 24 | +import { latestVersions } from '../utility/latest-versions'; |
| 25 | +import { getWorkspace } from '../utility/workspace'; |
| 26 | +import { Builders } from '../utility/workspace-models'; |
| 27 | +import { Schema as VitestBrowserOptions } from './schema'; |
| 28 | + |
| 29 | +export default function (options: VitestBrowserOptions): Rule { |
| 30 | + return async (host: Tree, _context: SchematicContext) => { |
| 31 | + const workspace = await getWorkspace(host); |
| 32 | + const project = workspace.projects.get(options.project); |
| 33 | + |
| 34 | + if (!project) { |
| 35 | + throw new SchematicsException(`Project "${options.project}" does not exist.`); |
| 36 | + } |
| 37 | + |
| 38 | + const testTarget = project.targets.get('test'); |
| 39 | + if (testTarget?.builder !== Builders.BuildUnitTest) { |
| 40 | + throw new SchematicsException( |
| 41 | + `Project "${options.project}" does not have a "test" target with a supported builder.`, |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + if (testTarget.options?.['runner'] === 'karma') { |
| 46 | + throw new SchematicsException( |
| 47 | + `Project "${options.project}" is configured to use Karma. ` + |
| 48 | + 'Please migrate to Vitest before adding browser testing support.', |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + const packageName = options.package; |
| 53 | + if (!packageName) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const dependencies = [packageName]; |
| 58 | + if (packageName === '@vitest/browser-playwright') { |
| 59 | + dependencies.push('playwright'); |
| 60 | + } else if (packageName === '@vitest/browser-webdriverio') { |
| 61 | + dependencies.push('webdriverio'); |
| 62 | + } |
| 63 | + |
| 64 | + // Update tsconfig.spec.json |
| 65 | + const tsConfigPath = |
| 66 | + (testTarget.options?.['tsConfig'] as string | undefined) ?? |
| 67 | + join(project.root, 'tsconfig.spec.json'); |
| 68 | + const updateTsConfigRule: Rule = (host) => { |
| 69 | + if (host.exists(tsConfigPath)) { |
| 70 | + const json = new JSONFile(host, tsConfigPath); |
| 71 | + const typesPath = ['compilerOptions', 'types']; |
| 72 | + const existingTypes = (json.get(typesPath) as string[] | undefined) ?? []; |
| 73 | + const newTypes = existingTypes.filter((t) => t !== 'jasmine'); |
| 74 | + |
| 75 | + if (!newTypes.includes('vitest/globals')) { |
| 76 | + newTypes.push('vitest/globals'); |
| 77 | + } |
| 78 | + |
| 79 | + if (packageName && !newTypes.includes(packageName)) { |
| 80 | + newTypes.push(packageName); |
| 81 | + } |
| 82 | + |
| 83 | + if ( |
| 84 | + newTypes.length !== existingTypes.length || |
| 85 | + newTypes.some((t, i) => t !== existingTypes[i]) |
| 86 | + ) { |
| 87 | + json.modify(typesPath, newTypes); |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + return chain([ |
| 93 | + updateTsConfigRule, |
| 94 | + ...dependencies.map((name) => |
| 95 | + addDependency(name, latestVersions[name], { |
| 96 | + type: DependencyType.Dev, |
| 97 | + existing: ExistingBehavior.Skip, |
| 98 | + install: options.skipInstall ? InstallBehavior.None : InstallBehavior.Auto, |
| 99 | + }), |
| 100 | + ), |
| 101 | + ]); |
| 102 | + }; |
| 103 | +} |
0 commit comments