Skip to content

Commit 134f58e

Browse files
committed
build: add 'update' option for Vitest runner and corresponding tests
Fixes #32218
1 parent f2917f5 commit 134f58e

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
describe('Vitest Runner', () => {
24+
it('should work with update flag enabled', async () => {
25+
harness.useTarget('test', {
26+
...BASE_OPTIONS,
27+
update: true,
28+
});
29+
30+
const { result } = await harness.executeOnce();
31+
32+
expect(result?.success).toBeTrue();
33+
});
34+
35+
it('should work with update flag disabled', async () => {
36+
harness.useTarget('test', {
37+
...BASE_OPTIONS,
38+
update: false,
39+
});
40+
41+
const { result } = await harness.executeOnce();
42+
43+
expect(result?.success).toBeTrue();
44+
});
45+
46+
it('should work without update flag (default)', async () => {
47+
harness.useTarget('test', {
48+
...BASE_OPTIONS,
49+
});
50+
51+
const { result } = await harness.executeOnce();
52+
53+
expect(result?.success).toBeTrue();
54+
});
55+
});
56+
57+
describe('Karma Runner', () => {
58+
it('should throw an error when update is used with karma', async () => {
59+
harness.useTarget('test', {
60+
...BASE_OPTIONS,
61+
runner: 'karma',
62+
update: true,
63+
});
64+
65+
const { result, error } = await harness.executeOnce({ outputLogsOnException: false });
66+
67+
expect(result).toBeUndefined();
68+
expect(error?.message).toContain(
69+
'The "update" option is only available for the "vitest" runner.',
70+
);
71+
});
72+
});
73+
});
74+
});

0 commit comments

Comments
 (0)