Skip to content

feat(@angular/build): add reporter output file option for unit-test #30682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions goldens/public-api/angular/build/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export type UnitTestBuilderOptions = {
debug?: boolean;
exclude?: string[];
include?: string[];
outputFile?: string;
providersFile?: string;
reporters?: string[];
runner: Runner;
Expand Down
1 change: 1 addition & 0 deletions packages/angular/build/src/builders/unit-test/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export async function* execute(
name: 'base',
include: [],
reporters: normalizedOptions.reporters ?? ['default'],
outputFile: normalizedOptions.outputFile,
watch: normalizedOptions.watch,
coverage: generateCoverageOption(
normalizedOptions.codeCoverage,
Expand Down
3 changes: 2 additions & 1 deletion packages/angular/build/src/builders/unit-test/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function normalizeOptions(
const buildTargetSpecifier = options.buildTarget ?? `::development`;
const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build');

const { tsConfig, runner, reporters, browsers } = options;
const { tsConfig, runner, reporters, outputFile, browsers } = options;

return {
// Project/workspace information
Expand All @@ -58,6 +58,7 @@ export async function normalizeOptions(
: undefined,
tsConfig,
reporters,
outputFile,
browsers,
watch: options.watch ?? isTTY(),
debug: options.debug ?? false,
Expand Down
4 changes: 4 additions & 0 deletions packages/angular/build/src/builders/unit-test/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
"type": "string"
}
},
"outputFile": {
"type": "string",
"description": "The file to output the test report to. If not specified, the test runner will output to the console."
},
"providersFile": {
"type": "string",
"description": "TypeScript file that exports an array of Angular providers to use during test execution. The array must be a default export.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { execute } from '../../index';
import {
BASE_OPTIONS,
describeBuilder,
UNIT_TEST_BUILDER_INFO,
setupApplicationTarget,
} from '../setup';

describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
describe('Options: "reporter" and "outputFile"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
});

it(`should output a JSON report`, async () => {
await harness.removeFile('src/app/app.component.spec.ts');
await harness.writeFiles({
'src/app/services/test.service.spec.ts': `
describe('TestService', () => {
it('should succeed', () => {
expect(true).toBe(true);
});
});`,
});

harness.useTarget('test', {
...BASE_OPTIONS,
reporters: ['json'],
outputFile: 'test-report.json',
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
const reportContent = await harness.readFile('test-report.json');
expect(reportContent).toContain('TestService');
});
});
});