Skip to content

Commit ce28407

Browse files
committed
fix(@angular/build): provide direct debugging support for unit test builder
When using the experimental `unit-test` builder with the `vitest` runner, a `debug` option is now available which configures the underlying runner to setup and use the Node Inspector to debug tests. This reduces the complexity to support debugging unit tests via a command execution of `ng test --debug`. This will cause the test process to initialize and then wait for a debugger to connect prior to executing the tests.
1 parent f9b9e4e commit ce28407

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

packages/angular/build/src/builders/unit-test/builder.ts

+19
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export async function* execute(
183183
// Setup vitest browser options if configured
184184
const { browser, errors } = setupBrowserConfiguration(
185185
normalizedOptions.browsers,
186+
normalizedOptions.debug,
186187
projectSourceRoot,
187188
);
188189
if (errors?.length) {
@@ -196,6 +197,13 @@ export async function* execute(
196197
if (buildTargetOptions?.polyfills?.length) {
197198
setupFiles.push('polyfills.js');
198199
}
200+
const debugOptions = normalizedOptions.debug
201+
? {
202+
inspectBrk: true,
203+
isolate: false,
204+
fileParallelism: false,
205+
}
206+
: {};
199207

200208
try {
201209
for await (const result of buildApplicationInternal(buildOptions, context, extensions)) {
@@ -226,6 +234,7 @@ export async function* execute(
226234
exclude: normalizedOptions.codeCoverageExclude,
227235
excludeAfterRemap: true,
228236
},
237+
...debugOptions,
229238
},
230239
});
231240

@@ -259,6 +268,7 @@ function findBrowserProvider(
259268

260269
function setupBrowserConfiguration(
261270
browsers: string[] | undefined,
271+
debug: boolean,
262272
projectSourceRoot: string,
263273
): { browser?: import('vitest/node').BrowserConfigOptions; errors?: string[] } {
264274
if (browsers === undefined) {
@@ -287,6 +297,15 @@ function setupBrowserConfiguration(
287297
);
288298
}
289299

300+
// Vitest current requires the playwright browser provider to use the inspect-brk option used by "debug"
301+
if (debug && provider !== 'playwright') {
302+
errors ??= [];
303+
errors.push(
304+
'Debugging browser mode tests currently requires the use of "playwright".' +
305+
' Please install this package and rerun the test command.',
306+
);
307+
}
308+
290309
if (errors) {
291310
return { errors };
292311
}

packages/angular/build/src/builders/unit-test/karma-bridge.ts

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ export async function useKarmaBuilder(
1515
context: BuilderContext,
1616
unitTestOptions: NormalizedUnitTestOptions,
1717
): Promise<AsyncIterable<BuilderOutput>> {
18+
if (unitTestOptions.debug) {
19+
context.logger.warn(
20+
'The "karma" test runner does not support the "debug" option. The option will be ignored.',
21+
);
22+
}
23+
1824
const buildTargetOptions = (await context.validateOptions(
1925
await context.getTargetOptions(unitTestOptions.buildTarget),
2026
await context.getBuilderNameForTarget(unitTestOptions.buildTarget),

packages/angular/build/src/builders/unit-test/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export async function normalizeOptions(
5252
reporters,
5353
browsers,
5454
watch,
55+
debug: options.debug ?? false,
5556
providersFile: options.providersFile && path.join(workspaceRoot, options.providersFile),
5657
};
5758
}

packages/angular/build/src/builders/unit-test/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
"type": "boolean",
4747
"description": "Run build when files change."
4848
},
49+
"debug": {
50+
"type": "boolean",
51+
"description": "Initialize the test runner to support using the Node Inspector for test debugging.",
52+
"default": false
53+
},
4954
"codeCoverage": {
5055
"type": "boolean",
5156
"description": "Output a code coverage report.",

0 commit comments

Comments
 (0)