Skip to content

Commit 9e02e28

Browse files
committed
build: add 'update' option for Vitest runner and corresponding tests
Fixes #32218
1 parent d9cd609 commit 9e02e28

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

goldens/public-api/angular/build/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ export type UnitTestBuilderOptions = {
242242
setupFiles?: string[];
243243
tsConfig?: string;
244244
ui?: boolean;
245+
update?: boolean;
245246
watch?: boolean;
246247
};
247248

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,16 @@ export async function normalizeOptions(
5454
const buildTargetSpecifier = options.buildTarget ?? `::development`;
5555
const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build');
5656

57-
const { runner, browsers, progress, filter, browserViewport, ui, runnerConfig } = options;
57+
const { runner, browsers, progress, filter, browserViewport, ui, runnerConfig, update } = options;
5858

5959
if (ui && runner !== Runner.Vitest) {
6060
throw new Error('The "ui" option is only available for the "vitest" runner.');
6161
}
6262

63+
if (update && runner !== Runner.Vitest) {
64+
throw new Error('The "update" option is only available for the "vitest" runner.');
65+
}
66+
6367
const [width, height] = browserViewport?.split('x').map(Number) ?? [];
6468

6569
let tsConfig = options.tsConfig;
@@ -132,6 +136,7 @@ export async function normalizeOptions(
132136
? true
133137
: path.resolve(workspaceRoot, runnerConfig)
134138
: runnerConfig,
139+
update: update ?? false,
135140
};
136141
}
137142

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ export class VitestExecutor implements TestExecutor {
238238
watch,
239239
...(typeof ui === 'boolean' ? { ui } : {}),
240240
...debugOptions,
241+
update: this.options.update,
241242
},
242243
{
243244
// Note `.vitest` is auto appended to the path.

packages/angular/build/src/builders/unit-test/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@
269269
"description": "Dumps build output files to the `.angular/cache` directory for debugging purposes.",
270270
"default": false,
271271
"visible": false
272+
},
273+
"update": {
274+
"type": "boolean",
275+
"description": "Updates test snapshots to match the current test output. This option is only available for the Vitest runner.",
276+
"default": false
272277
}
273278
},
274279
"additionalProperties": false,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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('Option: "update"', () => {
19+
beforeEach(() => {
20+
setupApplicationTarget(harness);
21+
});
22+
23+
it('should work with update flag enabled', async () => {
24+
harness.useTarget('test', {
25+
...BASE_OPTIONS,
26+
update: true,
27+
});
28+
29+
const { result } = await harness.executeOnce();
30+
31+
expect(result?.success).toBeTrue();
32+
});
33+
34+
it('should work with update flag disabled', async () => {
35+
harness.useTarget('test', {
36+
...BASE_OPTIONS,
37+
update: false,
38+
});
39+
40+
const { result } = await harness.executeOnce();
41+
42+
expect(result?.success).toBeTrue();
43+
});
44+
45+
it('should work without update flag (default)', async () => {
46+
harness.useTarget('test', {
47+
...BASE_OPTIONS,
48+
});
49+
50+
const { result } = await harness.executeOnce();
51+
52+
expect(result?.success).toBeTrue();
53+
});
54+
});
55+
});

0 commit comments

Comments
 (0)