Skip to content

Commit aea6f85

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

File tree

4 files changed

+87
-1
lines changed

4 files changed

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

0 commit comments

Comments
 (0)