Skip to content

Commit 7c92fb4

Browse files
committed
fix(@angular/build): defer empty test discovery to Vitest
Remove the unit-test builder pre-check that failed before Vitest ran so passWithNoTests in runner config can succeed in CI when no specs exist yet.
1 parent 92bb487 commit 7c92fb4

2 files changed

Lines changed: 58 additions & 8 deletions

File tree

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,6 @@ export async function getVitestBuildOptions(
195195

196196
// Find test files
197197
const testFiles = await findTests(include, exclude, workspaceRoot, projectSourceRoot);
198-
if (testFiles.length === 0) {
199-
throw new Error(
200-
'No tests found matching the following patterns:\n' +
201-
`- Included: ${include.join(', ')}\n` +
202-
(exclude.length ? `- Excluded: ${exclude.join(', ')}\n` : '') +
203-
`\nPlease check the 'test' target configuration in your project's 'angular.json' file.`,
204-
);
205-
}
206198

207199
const entryPoints = getTestEntrypoints(testFiles, {
208200
projectSourceRoot,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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('Behavior: no test files', () => {
19+
beforeEach(() => {
20+
setupApplicationTarget(harness);
21+
});
22+
23+
it('should fail when no tests are discovered and passWithNoTests is not enabled', async () => {
24+
harness.useTarget('test', {
25+
...BASE_OPTIONS,
26+
include: ['**/__no_matching_tests__.spec.ts'],
27+
});
28+
29+
const { result } = await harness.executeOnce();
30+
31+
expect(result?.success).toBeFalse();
32+
});
33+
34+
it('should succeed when no tests are discovered and passWithNoTests is enabled in runnerConfig', async () => {
35+
harness.useTarget('test', {
36+
...BASE_OPTIONS,
37+
include: ['**/__no_matching_tests__.spec.ts'],
38+
runnerConfig: 'vitest.config.mts',
39+
});
40+
41+
harness.writeFile(
42+
'vitest.config.mts',
43+
`
44+
import { defineConfig } from 'vitest/config';
45+
export default defineConfig({
46+
test: {
47+
passWithNoTests: true,
48+
},
49+
});
50+
`,
51+
);
52+
53+
const { result } = await harness.executeOnce();
54+
55+
expect(result?.success).toBeTrue();
56+
});
57+
});
58+
});

0 commit comments

Comments
 (0)