Skip to content

Commit 30352fc

Browse files
committed
fix(@angular/build): allow 'vitest' import in browser mode
This fixes the following error: 'The entry point "vitest" cannot be marked as external' by excluding vitest from the discovered dependencies handed to optimizeDeps.include Closes #31745
1 parent 72f2a46 commit 30352fc

File tree

2 files changed

+40
-1
lines changed
  • packages/angular/build/src/builders/unit-test/runners/vitest
  • tests/legacy-cli/e2e/tests/vitest

2 files changed

+40
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ export class VitestExecutor implements TestExecutor {
231231
coverage,
232232
projectName,
233233
projectSourceRoot: this.options.projectSourceRoot,
234-
optimizeDepsInclude: this.externalMetadata.explicitBrowser,
234+
optimizeDepsInclude: this.externalMetadata.explicitBrowser.filter(
235+
(entry) => entry !== 'vitest',
236+
),
235237
reporters,
236238
setupFiles: testSetupFiles,
237239
projectPlugins,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import assert from 'node:assert/strict';
2+
import { applyVitestBuilder } from '../../utils/vitest';
3+
import { ng, silentNpm, exec } from '../../utils/process';
4+
import { writeFile } from '../../utils/fs';
5+
import { updateJsonFile } from '../../utils/project';
6+
7+
/**
8+
* Allow `vitest` import in browser mode.
9+
* @see https://github.com/angular/angular-cli/issues/31745
10+
*/
11+
export default async function (): Promise<void> {
12+
await applyVitestBuilder();
13+
14+
await silentNpm('install', '@vitest/browser-playwright', 'playwright', '--save-dev');
15+
await exec('npx', 'playwright', 'install', 'chromium');
16+
17+
await updateJsonFile('angular.json', (json) => {
18+
const projects = Object.values(json['projects']);
19+
const project = projects[0]! as any;
20+
project['architect']['test']['options']['browsers'] = ['ChromiumHeadless'];
21+
});
22+
23+
await writeFile(
24+
'src/app/app.spec.ts',
25+
`
26+
import { test, expect } from 'vitest';
27+
28+
test('should pass', () => {
29+
expect(true).toBe(true);
30+
});
31+
`,
32+
);
33+
34+
const { stdout } = await ng('test');
35+
36+
assert.match(stdout, /1 passed/, 'Expected 1 tests to pass.');
37+
}

0 commit comments

Comments
 (0)