Skip to content

test(@angular/build): add unit tests for unit-test builder options #30766

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

Merged
merged 1 commit into from
Jul 24, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @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) => {
xdescribe('Option: "browsers"', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pending() worked here without failing the test suite? IIRC that's what I used for the disabled tests for the karma-on-application-builder feature.

beforeEach(async () => {
setupApplicationTarget(harness);
});

it('should use jsdom when browsers is not provided', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
browsers: undefined,
});

const { result, logs } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(logs).toContain(
jasmine.objectContaining({ message: 'Using jsdom in Node.js for test execution.' }),
);
});

it('should fail when browsers is empty', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
browsers: [],
});

await expectAsync(harness.executeOnce()).toBeRejectedWithError(
/must NOT have fewer than 1 items/,
);
});

it('should launch a browser when provided', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
browsers: ['chrome'],
});

const { result, logs } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(logs).toContain(
jasmine.objectContaining({ message: jasmine.stringMatching(/Starting browser "chrome"/) }),
);
});

it('should launch a browser in headless mode when specified', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
browsers: ['chromeheadless'],
});

const { result, logs } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(logs).toContain(
jasmine.objectContaining({
message: jasmine.stringMatching(/Starting browser "chrome" in headless mode/),
}),
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @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) => {
xdescribe('Option: "buildTarget"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
});

it('should fail when buildTarget is not provided', async () => {
const { buildTarget, ...rest } = BASE_OPTIONS;
harness.useTarget('test', rest as any);

await expectAsync(harness.executeOnce()).toBeRejectedWithError(/"buildTarget" is required/);
});

it('should fail when buildTarget is empty', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
buildTarget: '',
});

await expectAsync(harness.executeOnce()).toBeRejectedWithError(
/must match "\^\S+:\S+(:\S+)?\$"/,
);
});

it('should fail when buildTarget does not have a project name', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
buildTarget: ':build',
});

await expectAsync(harness.executeOnce()).toBeRejectedWithError(
/must match "\^\S+:\S+(:\S+)?\$"/,
);
});

it('should fail when buildTarget does not have a target name', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
buildTarget: 'app:',
});

await expectAsync(harness.executeOnce()).toBeRejectedWithError(
/must match "\^\S+:\S+(:\S+)?\$"/,
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @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) => {
xdescribe('Option: "codeCoverageExclude"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
await harness.writeFiles({
'src/app/error.ts': `export const a = 1;`,
});
});

it('should not exclude any files from coverage when not provided', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
codeCoverage: true,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
const summary = harness.readFile('coverage/coverage-summary.json');
expect(summary).toContain('"src/app/error.ts"');
});

it('should exclude files from coverage that match the glob pattern', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
codeCoverage: true,
codeCoverageExclude: ['**/error.ts'],
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
const summary = harness.readFile('coverage/coverage-summary.json');
expect(summary).not.toContain('"src/app/error.ts"');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @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) => {
xdescribe('Option: "codeCoverageReporters"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
});

it('should generate a json summary report when specified', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
codeCoverage: true,
codeCoverageReporters: ['json-summary'] as any,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(harness.hasFile('coverage/coverage-summary.json')).toBeTrue();
});

it('should generate multiple reports when specified', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
codeCoverage: true,
codeCoverageReporters: ['json-summary', 'lcov'] as any,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(harness.hasFile('coverage/coverage-summary.json')).toBeTrue();
expect(harness.hasFile('coverage/lcov.info')).toBeTrue();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @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) => {
xdescribe('Option: "codeCoverage"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
});

it('should not generate a code coverage report when codeCoverage is false', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
codeCoverage: false,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(harness.hasFile('coverage/index.html')).toBeFalse();
});

it('should generate a code coverage report when codeCoverage is true', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
codeCoverage: true,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(harness.hasFile('coverage/index.html')).toBeTrue();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @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) => {
xdescribe('Option: "debug"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
});

it('should not enter debug mode when debug is false', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
debug: false,
});

const { result, logs } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(logs).not.toContain(
jasmine.objectContaining({ message: jasmine.stringMatching(/Node.js inspector/) }),
);
});

it('should enter debug mode when debug is true', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
debug: true,
});

const { result, logs } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(logs).toContain(
jasmine.objectContaining({
message: jasmine.stringMatching(/Node.js inspector is active/),
}),
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @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) => {
xdescribe('Option: "providersFile"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
});

it('should fail when providersFile does not exist', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
providersFile: 'src/my.providers.ts',
});

const { result, error } = await harness.executeOnce({ outputLogsOnFailure: false });
expect(result).toBeUndefined();
expect(error?.message).toMatch(
`The specified providers file "src/my.providers.ts" does not exist.`,
);
});

it('should use providers from the specified file', async () => {
await harness.writeFiles({
'src/my.providers.ts': `
import { importProvidersFrom } from '@angular/core';
import { CommonModule } from '@angular/common';
export default [importProvidersFrom(CommonModule)];
`,
});

harness.useTarget('test', {
...BASE_OPTIONS,
providersFile: 'src/my.providers.ts',
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});
});
});
Loading
Loading