Skip to content

Commit 2b9be3a

Browse files
committed
fix(@angular/build): ensure correct project targeting during Vitest debugging
This addresses issue angular#31652 where debugging failed to target the correct project instance. When running browser tests, Vitest appends the browser name to the project identifier. This change updates the CLI to match that naming convention when initiating a debug session, ensuring the correct project is selected. Fixes angular#31652 (cherry picked from commit 32adc3a)
1 parent 8456da3 commit 2b9be3a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

packages/angular/build/src/builders/unit-test/runners/vitest/executor.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import type { BuilderOutput } from '@angular-devkit/architect';
9+
import type { BuilderContext, BuilderOutput } from '@angular-devkit/architect';
1010
import assert from 'node:assert';
11-
import path, { join } from 'node:path';
11+
import path from 'node:path';
1212
import type { Vitest } from 'vitest/node';
1313
import {
1414
DevServerExternalResultMetadata,
@@ -32,6 +32,7 @@ export class VitestExecutor implements TestExecutor {
3232
private normalizePath: ((id: string) => string) | undefined;
3333
private readonly projectName: string;
3434
private readonly options: NormalizedUnitTestBuilderOptions;
35+
private readonly logger: BuilderContext['logger'];
3536
private readonly buildResultFiles = new Map<string, ResultFile>();
3637
private readonly externalMetadata: DevServerExternalResultMetadata = {
3738
implicitBrowser: [],
@@ -51,9 +52,11 @@ export class VitestExecutor implements TestExecutor {
5152
projectName: string,
5253
options: NormalizedUnitTestBuilderOptions,
5354
testEntryPointMappings: Map<string, string> | undefined,
55+
logger: BuilderContext['logger'],
5456
) {
5557
this.projectName = projectName;
5658
this.options = options;
59+
this.logger = logger;
5760

5861
if (testEntryPointMappings) {
5962
for (const [entryPoint, testFile] of testEntryPointMappings) {
@@ -209,13 +212,26 @@ export class VitestExecutor implements TestExecutor {
209212
? await findVitestBaseConfig([projectRoot, workspaceRoot])
210213
: runnerConfig;
211214

215+
let project = projectName;
216+
if (debug && browserOptions.browser?.instances) {
217+
if (browserOptions.browser.instances.length > 1) {
218+
this.logger.warn(
219+
'Multiple browsers are configured, but only the first browser will be used for debugging.',
220+
);
221+
}
222+
223+
// When running browser tests, Vitest appends the browser name to the project identifier.
224+
// The project name must match this augmented name to ensure the correct project is targeted.
225+
project = `${projectName} (${browserOptions.browser.instances[0].browser})`;
226+
}
227+
212228
return startVitest(
213229
'test',
214230
undefined,
215231
{
216232
config: externalConfigPath,
217233
root: workspaceRoot,
218-
project: projectName,
234+
project,
219235
outputFile,
220236
cache: cacheOptions.enabled ? undefined : false,
221237
testNamePattern: this.options.filter,

packages/angular/build/src/builders/unit-test/runners/vitest/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const VitestTestRunner: TestRunner = {
6767
context.logger.info('Automatically searching for and using Vitest configuration file.');
6868
}
6969

70-
return new VitestExecutor(projectName, options, testEntryPointMappings);
70+
return new VitestExecutor(projectName, options, testEntryPointMappings, context.logger);
7171
},
7272
};
7373

0 commit comments

Comments
 (0)