I've just moved over to Angular 21 and started using Vitest.
I'd like to initialize one of my services before the tests starts.
So I added provideAppInitializer in src/test-providers.ts like so:
// Imports
const testProviders = [
provideHttpClient(),
provideHttpClientTesting(),
provideLuxonDateAdapter(),
provideAppInitializer(() => {
const currentService = inject(CurrentService);
currentService.setMe(mockMe());
currentService.setCustomer(1, mockCustomer());
}),
];
export default testProviders;
angular.json:
...
"test": {
"builder": "@angular/build:unit-test",
"options": {
"include": ["src/app/**/*.spec.ts"],
"providersFile": "src/test-providers.ts"
}
}
...
But then I end up with the error
✘ [ERROR] File 'src\test-providers.ts' not found in TypeScript compilation. [plugin angular-compiler]
I am also aware that there's something called setupFiles in angular.json, but I don't really know how to inject a service there.
How to inject and run a service before test starts?