Skip to content

Commit 087f61c

Browse files
committed
feat(@angular/build): add reporter output file option for unit-test
The experimental `unit-test` builder now contains an additional option named `outputFile` that supports configuring the output file for the reporter. This option is currently only used by the `vitest` runner. See https://vitest.dev/config/#outputfile Usage example: ``` ng test --no-watch --reporter=json --output-file=results/unit-test.json ```
1 parent 568e5d2 commit 087f61c

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

goldens/public-api/angular/build/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export type UnitTestBuilderOptions = {
223223
debug?: boolean;
224224
exclude?: string[];
225225
include?: string[];
226+
outputFile?: string;
226227
providersFile?: string;
227228
reporters?: string[];
228229
runner: Runner;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ export async function* execute(
239239
name: 'base',
240240
include: [],
241241
reporters: normalizedOptions.reporters ?? ['default'],
242+
outputFile: normalizedOptions.outputFile,
242243
watch: normalizedOptions.watch,
243244
coverage: generateCoverageOption(
244245
normalizedOptions.codeCoverage,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export async function normalizeOptions(
3333
const buildTargetSpecifier = options.buildTarget ?? `::development`;
3434
const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build');
3535

36-
const { tsConfig, runner, reporters, browsers } = options;
36+
const { tsConfig, runner, reporters, outputFile, browsers } = options;
3737

3838
return {
3939
// Project/workspace information
@@ -58,6 +58,7 @@ export async function normalizeOptions(
5858
: undefined,
5959
tsConfig,
6060
reporters,
61+
outputFile,
6162
browsers,
6263
watch: options.watch ?? isTTY(),
6364
debug: options.debug ?? false,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595
"type": "string"
9696
}
9797
},
98+
"outputFile": {
99+
"type": "string",
100+
"description": "The file to output the test report to. If not specified, the test runner will output to the console."
101+
},
98102
"providersFile": {
99103
"type": "string",
100104
"description": "TypeScript file that exports an array of Angular providers to use during test execution. The array must be a default export.",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 { execute } from '../../index';
10+
import {
11+
BASE_OPTIONS,
12+
describeBuilder,
13+
UNIT_TEST_BUILDER_INFO,
14+
setupApplicationTarget,
15+
} from '../setup';
16+
17+
describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
18+
describe('Options: "reporter" and "outputFile"', () => {
19+
beforeEach(async () => {
20+
setupApplicationTarget(harness);
21+
});
22+
23+
it(`should output a JSON report`, async () => {
24+
await harness.removeFile('src/app/app.component.spec.ts');
25+
await harness.writeFiles({
26+
'src/app/services/test.service.spec.ts': `
27+
describe('TestService', () => {
28+
it('should succeed', () => {
29+
expect(true).toBe(true);
30+
});
31+
});`,
32+
});
33+
34+
harness.useTarget('test', {
35+
...BASE_OPTIONS,
36+
reporters: ['json'],
37+
outputFile: 'test-report.json',
38+
});
39+
40+
const { result } = await harness.executeOnce();
41+
expect(result?.success).toBeTrue();
42+
const reportContent = await harness.readFile('test-report.json');
43+
expect(reportContent).toContain('TestService');
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)