Skip to content

Commit 43a3111

Browse files
clydinalan-agius4
authored andcommitted
test(@angular/build): add initial unit-test vitest runner E2E tests
Two E2E tests have been added to test initial capabilities of the experimental unit-test builder with the vitest runner. This also ensures that the generated tests are compatible with both the karma/jasmine-based runner and the vitest runner.
1 parent b8eada5 commit 43a3111

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

tests/legacy-cli/e2e.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ ESBUILD_TESTS = [
3737
"tests/commands/serve/ssr-http-requests-assets.js",
3838
"tests/i18n/**",
3939
"tests/vite/**",
40+
"tests/vitest/**",
4041
"tests/test/**",
4142
]
4243

4344
WEBPACK_IGNORE_TESTS = [
4445
"tests/vite/**",
46+
"tests/vitest/**",
4547
"tests/build/app-shell/**",
4648
"tests/i18n/ivy-localize-app-shell.js",
4749
"tests/i18n/ivy-localize-app-shell-service-worker.js",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import assert from 'node:assert/strict';
2+
import { applyVitestBuilder } from '../../utils/vitest';
3+
import { ng } from '../../utils/process';
4+
5+
export default async function (): Promise<void> {
6+
await applyVitestBuilder();
7+
8+
const { stderr } = await ng('test');
9+
10+
assert.match(
11+
stderr,
12+
/NOTE: The "unit-test" builder is currently EXPERIMENTAL/,
13+
'Expected stderr to include the experimental notice.',
14+
);
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import assert from 'node:assert/strict';
2+
import { applyVitestBuilder } from '../../utils/vitest';
3+
import { ng } from '../../utils/process';
4+
5+
export default async function (): Promise<void> {
6+
await applyVitestBuilder();
7+
await ng('generate', 'component', 'my-comp');
8+
9+
const { stdout } = await ng('test');
10+
11+
assert.match(stdout, /2 passed/, 'Expected 2 tests to pass.');
12+
}

tests/legacy-cli/e2e/utils/vitest.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { silentNpm } from './process';
2+
import { updateJsonFile } from './project';
3+
4+
/** Updates the `test` builder in the current workspace to use Vitest. */
5+
export async function applyVitestBuilder(): Promise<void> {
6+
await silentNpm('install', 'vitest@3.2.4', 'jsdom@26.1.0', '--save-dev');
7+
8+
await updateJsonFile('angular.json', (json) => {
9+
const projects = Object.values(json['projects']);
10+
if (projects.length !== 1) {
11+
throw new Error(
12+
`Expected exactly one project but found ${projects.length} projects named ${Object.keys(
13+
json['projects'],
14+
).join(', ')}`,
15+
);
16+
}
17+
const project = projects[0]! as any;
18+
19+
// Update to Vitest builder.
20+
const test = project['architect']['test'];
21+
test['builder'] = '@angular/build:unit-test';
22+
test['options'] = {
23+
tsConfig: test['options']['tsConfig'],
24+
buildTarget: '::development',
25+
runner: 'vitest',
26+
};
27+
});
28+
}

0 commit comments

Comments
 (0)