How to unit test AuthService in Angular SDK

I have followed the documentation on https://auth0.com/docs/libraries/auth0-angular-spa but there is no documentation on writing unit tests.

I have tried defining authService in my spec file as follows:
beforeEach(() => {
authService = TestBed.inject(AuthService);
}

But I get the following error:
NullInjectorError: R3InjectorError(DynamicTestModule)[AuthService -> InjectionToken auth0.client -> InjectionToken auth0.client]:
NullInjectorError: No provider for InjectionToken auth0.client!

Please let me know how to test the Angular SDK AuthService

1 Like

Hey there!

We don’t have specific guidance on how to run tests for specific parts of our stack. We only offer general guidance on testing your integrations with Auth0 stack. More on that here:

Hello, thank you for the reply.

Is there a way to write the unit test cases for @auth0/auth0-angular SDK or are there any examples on this? I need to be able to mock the AuthService or test it in any possible way. I could not find anything on this based on the links.

As I said above we don’t have content for each specific part of the stack, rather general content on testing with Auth0.

@Kia.kia Hello, have you solved this somehow? I have the same issue

@Kia.kia & @andrew.p1 (and all others coming across this later):
I could solve this by making sure that I have the AuthModule.forRoot({...}) also in the imports array of the TestBed of each *.spec.ts file:

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        ...,
        AuthModule.forRoot({
          domain: 'YOURTENANTDOMAIN.DATACENTER.auth0.com',
          clientId: '...',
        }),
      ],
    }).compileComponents(); // edit 1: this compileComponents() call is only needed if you have to import other components that require it to run the test

In further development of my current project I will try to learn more if you need to have valid credentials in there or not. I hope I will not forget to update this once I have done a bit more with it.

@konrad.sopala I am slightly disappointed that you don’t find anything about this in the Quickstarters that are provided - no hints, no examples, no best practices. Unit testing is nothing new or fancy, so having it included in your guides would be an improvement that we should not need to ask for…

1 Like

Hey there Manuel! Totally understand that but in order to fight for it for you it will be best if you can create such topic providing all the necessary context in our Feedback category here:

1 Like

Did so, can be found here: Include unit-testing examples / best practices in documentation

1 Like

Thanks a lot for that!