0

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?

1
  • do yo have file src\test-providers.ts? can you share your tsconfig files if yes? Commented Nov 26 at 10:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.