Skip to content

Commit ceaf6b5

Browse files
committed
fix(@angular/build): allow TestBed provider configuration with vitest unit-testing
The experimental `unit-test` builder with the `vitest` runner configured can now specify an Angular providers file via the `providersFile` option. This allows the TestBed initialization to use the providers defined in this file for all executed tests. The contents of the providers file should include a default export with an array of one or more Angular providers. As an example: ``` import { provideZonelessChangeDetection } from '@angular/core'; export default [ provideZonelessChangeDetection(), ]; ```
1 parent b37df3a commit ceaf6b5

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,28 @@ export async function* execute(
142142
loadContent: async () => {
143143
const contents: string[] = [
144144
// Initialize the Angular testing environment
145+
`import { NgModule } from '@angular/core';`,
145146
`import { getTestBed, ɵgetCleanupHook as getCleanupHook } from '@angular/core/testing';`,
146147
`import { BrowserTestingModule, platformBrowserTesting } from '@angular/platform-browser/testing';`,
147148
`import { beforeEach, afterEach } from 'vitest';`,
148149
'',
150+
normalizedOptions.providersFile
151+
? `import providers from './${path
152+
.relative(projectSourceRoot, normalizedOptions.providersFile)
153+
.replace(/.[mc]?ts$/, '')
154+
.replace(/\\/g, '/')}'`
155+
: 'const providers = [];',
156+
'',
149157
// Same as https://github.com/angular/angular/blob/05a03d3f975771bb59c7eefd37c01fa127ee2229/packages/core/testing/src/test_hooks.ts#L21-L29
150158
`beforeEach(getCleanupHook(false));`,
151159
`afterEach(getCleanupHook(true));`,
152160
'',
153-
`getTestBed().initTestEnvironment(BrowserTestingModule, platformBrowserTesting(), {`,
161+
`@NgModule({`,
162+
` providers,`,
163+
`})`,
164+
`export class TestModule {}`,
165+
'',
166+
`getTestBed().initTestEnvironment([BrowserTestingModule, TestModule], platformBrowserTesting(), {`,
154167
` errorOnUnknownElements: true,`,
155168
` errorOnUnknownProperties: true,`,
156169
'});',

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

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export async function normalizeOptions(
5252
reporters,
5353
browsers,
5454
watch,
55+
providersFile: options.providersFile && path.join(workspaceRoot, options.providersFile),
5556
};
5657
}
5758

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

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
"items": {
6666
"type": "string"
6767
}
68+
},
69+
"providersFile": {
70+
"type": "string",
71+
"description": "TypeScript file that exports an array of Angular providers to use during test execution. The array must be a default export.",
72+
"minLength": 1
6873
}
6974
},
7075
"additionalProperties": false,

0 commit comments

Comments
 (0)