angular/angular-cli · error · SchematicsException
Cannot add a Vitest configuration as builder for "test" targ
Error message
Cannot add a Vitest configuration as builder for "test" target in project does not use "${AngularBuilder.BuildUnitTest}". What it means
Even when a `test` target exists, `addVitestConfig` only supports projects whose test target uses the `@angular/build:unit-test` builder (`AngularBuilder.BuildUnitTest`). If the target uses any other builder (e.g. `@angular-devkit/build-angular:karma`), the schematic refuses to add a Vitest configuration because the generated `vitest-base.config.ts` would not be consumed. The grammatically garbled message means: the test target's builder must be the unit-test builder.
Source
Thrown at packages/schematics/angular/config/index.ts:62
function addVitestConfig(options: ConfigOptions): Rule {
return (tree, context) =>
updateWorkspace((workspace) => {
const project = workspace.projects.get(options.project);
if (!project) {
throw new SchematicsException(`Project name "${options.project}" doesn't not exist.`);
}
const testTarget = project.targets.get('test');
if (!testTarget) {
throw new SchematicsException(
`No "test" target found for project "${options.project}".` +
' A "test" target is required to generate a Vitest configuration.',
);
}
if (testTarget.builder !== AngularBuilder.BuildUnitTest) {
throw new SchematicsException(
`Cannot add a Vitest configuration as builder for "test" target in project does not` +
` use "${AngularBuilder.BuildUnitTest}".`,
);
}
testTarget.options ??= {};
testTarget.options.runnerConfig = true;
// Check runner option.
if (testTarget.options.runner === 'karma') {
context.logger.warn(
`The "test" target is configured to use the "karma" runner in the main options.` +
' The generated "vitest-base.config.ts" file may not be used.',
);
}
for (const [name, config] of Object.entries(testTarget.configurations ?? {})) {
if (View on GitHub (pinned to bb72145f9a)
Solutions
- Change the test target builder in angular.json to `@angular/build:unit-test`, then re-run `ng generate config <project> --type vitest`.
- Run the Angular CLI migration to the application builder / unit-test builder (`ng update @angular/cli`) if you are on an older workspace.
- If you intend to stay on Karma, generate a karma configuration instead (`--type karma`) rather than a Vitest one.
Example fix
// angular.json before
"test": { "builder": "@angular-devkit/build-angular:karma", "options": { ... } }
// after
"test": { "builder": "@angular/build:unit-test", "options": { ... } } Defensive patterns
Strategy: validation
Validate before calling
const testTarget = project.architect?.test;
if (testTarget && testTarget.builder !== '@angular/build:unit-test') {
throw new Error(`Test target builder is "${testTarget.builder}"; Vitest config requires "@angular/build:unit-test".`);
} Type guard
function usesUnitTestBuilder(t: { builder?: string } | undefined): boolean {
return t?.builder === '@angular/build:unit-test';
} Try / catch
try {
await runSchematic('config', { project, type: 'vitest' });
} catch (e) {
if (e instanceof SchematicsException && e.message.includes('Cannot add a Vitest configuration')) {
console.error('Switch the test target builder to @angular/build:unit-test first.');
} else throw e;
} Prevention
- Complete `ng update @angular/cli` migrations before generating Vitest configs.
- Keep the test target on `@angular/build:unit-test` in all workspace projects.
- Never point the test target at the legacy karma builder if you plan to use Vitest.
When it happens
Trigger: Running `ng generate config <project> --type vitest` when the project's `test` target builder is `@angular-devkit/build-angular:karma` or any builder other than `@angular/build:unit-test`.
Common situations: Projects migrated from Karma but still on the legacy karma builder; workspaces mixing old and new builder versions; users who upgraded Angular CLI partially so the new unit-test builder is unavailable; manually authored test targets pointing at a different builder.
Related errors
- No "test" target found for project "${options.project}". A "
- Cannot add a karma configuration as builder for "test" targe
- Project "${projectName}" uses a custom coverage reporter "${
- Project "${projectName}" uses a custom Karma configuration f
- The following Karma configuration files require manual migr
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/77f8db1af978e0af.
Report an issue: GitHub.