fix(angular): install @angular/build when using vitest test runner (#31169)

## Current Behavior

The application and library generators do not install the
`@angular/build` package when using the `vitest` test runner. This can
cause resolution issues depending on the package manager because the
package is a peer dependency of the `@analogjs/vite-plugin-angular`
package.

## Expected Behavior

The application and library generators should install the
`@angular/build` package when using the `vitest` test runner.

## Related Issue(s)

Fixes #30646
This commit is contained in:
Leosvel Pérez Espinosa 2025-05-13 13:22:49 +02:00 committed by GitHub
parent 8cf4b55174
commit e1c0be3d2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -1,5 +1,10 @@
import { ensurePackage, type Tree } from '@nx/devkit';
import {
addDependenciesToPackageJson,
ensurePackage,
type Tree,
} from '@nx/devkit';
import { nxVersion } from '../../utils/versions';
import { getInstalledAngularDevkitVersion, versions } from './version-utils';
export type AddVitestOptions = {
name: string;
@ -25,4 +30,18 @@ export async function addVitest(
coverageProvider: 'v8',
addPlugin: options.addPlugin ?? false,
});
if (!options.skipPackageJson) {
const angularDevkitVersion =
getInstalledAngularDevkitVersion(tree) ??
versions(tree).angularDevkitVersion;
addDependenciesToPackageJson(
tree,
{},
{ '@angular/build': angularDevkitVersion },
undefined,
true
);
}
}

View File

@ -8,6 +8,13 @@ import {
import * as latestVersions from '../../utils/versions';
import { angularVersion } from '../../utils/versions';
export function getInstalledAngularDevkitVersion(tree: Tree): string | null {
return (
getInstalledPackageVersion(tree, '@angular-devkit/build-angular') ??
getInstalledPackageVersion(tree, '@angular/build')
);
}
export function getInstalledAngularVersion(tree: Tree): string {
const pkgJson = readJson(tree, 'package.json');
const installedAngularVersion =