diff --git a/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts b/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts index efff72e5d8bb..03d402a6802d 100644 --- a/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts +++ b/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts @@ -195,14 +195,6 @@ export async function getVitestBuildOptions( // Find test files const testFiles = await findTests(include, exclude, workspaceRoot, projectSourceRoot); - if (testFiles.length === 0) { - throw new Error( - 'No tests found matching the following patterns:\n' + - `- Included: ${include.join(', ')}\n` + - (exclude.length ? `- Excluded: ${exclude.join(', ')}\n` : '') + - `\nPlease check the 'test' target configuration in your project's 'angular.json' file.`, - ); - } const entryPoints = getTestEntrypoints(testFiles, { projectSourceRoot, diff --git a/packages/angular/build/src/builders/unit-test/tests/behavior/pass-with-no-tests_spec.ts b/packages/angular/build/src/builders/unit-test/tests/behavior/pass-with-no-tests_spec.ts new file mode 100644 index 000000000000..c3acc7df1770 --- /dev/null +++ b/packages/angular/build/src/builders/unit-test/tests/behavior/pass-with-no-tests_spec.ts @@ -0,0 +1,58 @@ +/** + * @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('Behavior: no test files', () => { + beforeEach(() => { + setupApplicationTarget(harness); + }); + + it('should fail when no tests are discovered and passWithNoTests is not enabled', async () => { + harness.useTarget('test', { + ...BASE_OPTIONS, + include: ['**/__no_matching_tests__.spec.ts'], + }); + + const { result } = await harness.executeOnce(); + + expect(result?.success).toBeFalse(); + }); + + it('should succeed when no tests are discovered and passWithNoTests is enabled in runnerConfig', async () => { + harness.useTarget('test', { + ...BASE_OPTIONS, + include: ['**/__no_matching_tests__.spec.ts'], + runnerConfig: 'vitest.config.mts', + }); + + harness.writeFile( + 'vitest.config.mts', + ` + import { defineConfig } from 'vitest/config'; + export default defineConfig({ + test: { + passWithNoTests: true, + }, + }); + `, + ); + + const { result } = await harness.executeOnce(); + + expect(result?.success).toBeTrue(); + }); + }); +});