Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export class VitestExecutor implements TestExecutor {

updateExternalMetadata(buildResult, this.externalMetadata, undefined, true);

// Reset the exit code to allow for a clean state.
// This is necessary because Vitest may set the exit code on failure, which can
// affect subsequent runs in watch mode or when running multiple builders.
process.exitCode = 0;

// Initialize Vitest if not already present.
this.vitest ??= await this.initializeVitest();
const vitest = this.vitest;
Expand Down Expand Up @@ -122,7 +127,17 @@ export class VitestExecutor implements TestExecutor {
// Check if all the tests pass to calculate the result
const testModules = testResults?.testModules ?? this.vitest.state.getTestModules();

yield { success: testModules.every((testModule) => testModule.ok()) };
let success = testModules.every((testModule) => testModule.ok());
// Vitest does not return a failure result when coverage thresholds are not met.
// Instead, it sets the process exit code to 1.
// We check this exit code to determine if the test run should be considered a failure.
if (success && process.exitCode === 1) {
success = false;
// Reset the exit code to prevent it from carrying over to subsequent runs/builds
process.exitCode = 0;
}

yield { success };
}

async [Symbol.asyncDispose](): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,16 @@ async function generateCoverageOption(
...(optionsCoverage.include
? { include: ['spec-*.js', 'chunk-*.js', ...optionsCoverage.include] }
: {}),
thresholds: optionsCoverage.thresholds,
watermarks: optionsCoverage.watermarks,
// The 'in' operator is used here because 'configCoverage' is a union type and
// not all coverage providers support thresholds or watermarks.
thresholds: mergeCoverageObjects(
configCoverage && 'thresholds' in configCoverage ? configCoverage.thresholds : undefined,
optionsCoverage.thresholds,
),
watermarks: mergeCoverageObjects(
configCoverage && 'watermarks' in configCoverage ? configCoverage.watermarks : undefined,
optionsCoverage.watermarks,
),
// Special handling for `exclude`/`reporters` due to an undefined value causing upstream failures
...(optionsCoverage.exclude
? {
Expand All @@ -388,3 +396,26 @@ async function generateCoverageOption(
: {}),
};
}

/**
* Merges coverage related objects while ignoring any `undefined` values.
* This ensures that Angular CLI options correctly override Vitest configuration
* only when explicitly provided.
*/
function mergeCoverageObjects<T extends object>(
configValue: T | undefined,
optionsValue: T | undefined,
): T | undefined {
if (optionsValue === undefined) {
return configValue;
}

const result: Record<string, unknown> = { ...(configValue ?? {}) };
for (const [key, value] of Object.entries(optionsValue)) {
if (value !== undefined) {
result[key] = value;
}
}

return Object.keys(result).length > 0 ? (result as T) : undefined;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* @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('Option: "runnerConfig" Coverage Merging', () => {
beforeEach(() => {
setupApplicationTarget(harness);
});

describe('Vitest Runner', () => {
it('should preserve thresholds from Vitest config when not overridden by CLI', async () => {
harness.writeFile(
'vitest-base.config.ts',
`
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
coverage: {
thresholds: {
branches: 100
}
}
}
});
`,
);

harness.useTarget('test', {
...BASE_OPTIONS,
runnerConfig: true,
coverage: true,
});

const { result } = await harness.executeOnce();

// Should fail because branches are not 100%
expect(result?.success).toBeFalse();
});

it('should override Vitest config thresholds with CLI thresholds', async () => {
harness.writeFile(
'vitest-base.config.ts',
`
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
coverage: {
thresholds: {
branches: 100
}
}
}
});
`,
);

harness.useTarget('test', {
...BASE_OPTIONS,
runnerConfig: true,
coverage: true,
coverageThresholds: {
branches: 0,
},
});

const { result } = await harness.executeOnce();

// Should pass because CLI overrides threshold to 0
expect(result?.success).toBeTrue();
});

it('should merge partial CLI thresholds with Vitest config thresholds', async () => {
harness.writeFile(
'vitest-base.config.ts',
`
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
coverage: {
thresholds: {
statements: 100,
branches: 100
}
}
}
});
`,
);

harness.useTarget('test', {
...BASE_OPTIONS,
runnerConfig: true,
coverage: true,
coverageThresholds: {
statements: 0,
// branches is undefined here, should remain 100 from config
},
});

const { result } = await harness.executeOnce();

// Should still fail because branches threshold (100) is not met
expect(result?.success).toBeFalse();
});
});
});
});