Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5bccc81

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 d47386e commit 5bccc81

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
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
progress?: boolean;
227228
providersFile?: string;
228229
reporters?: string[];

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, progress } = options;
36+
const { tsConfig, runner, reporters, outputFile, browsers, progress } = options;
3737

3838
return {
3939
// Project/workspace information
@@ -59,6 +59,7 @@ export async function normalizeOptions(
5959
tsConfig,
6060
buildProgress: progress,
6161
reporters,
62+
outputFile,
6263
browsers,
6364
watch: options.watch ?? isTTY(),
6465
debug: options.debug ?? false,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ export class VitestExecutor implements TestExecutor {
135135
}
136136

137137
private async initializeVitest(): Promise<Vitest> {
138-
const { codeCoverage, reporters, workspaceRoot, browsers, debug, watch } = this.options;
139-
138+
const { codeCoverage, reporters, outputFile, workspaceRoot, browsers, debug, watch } =
139+
this.options;
140140
let vitestNodeModule;
141141
try {
142142
vitestNodeModule = await loadEsmModule<typeof import('vitest/node')>('vitest/node');
@@ -196,6 +196,7 @@ export class VitestExecutor implements TestExecutor {
196196
name: 'base',
197197
include: [],
198198
reporters: reporters ?? ['default'],
199+
outputFile,
199200
watch,
200201
coverage: generateCoverageOption(codeCoverage),
201202
...debugOptions,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@
9393
"type": "string"
9494
}
9595
},
96+
"outputFile": {
97+
"type": "string",
98+
"description": "The file to output the test report to. If not specified, the test runner will output to the console."
99+
},
96100
"providersFile": {
97101
"type": "string",
98102
"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)