-
Notifications
You must be signed in to change notification settings - Fork 93
-
Can we use the testing-library/angular for unit testing services as wel.
Like an AuthService
.
So without creating a 'login-component' or something to render()
and call the autService
Or in that case, just use 'plain' jest / Angular Testbed. Or even a separate library like https://ngneat.github.io/spectator/
The sample: https://github.com/testing-library/angular-testing-library/blob/main/apps/example-app/src/app/examples/12-service-component.spec.ts uses a Component to test/mock the service.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 1 reply
-
What is the advantage of this, and what would be the big difference of creating the service manually via its constructor?
Beta Was this translation helpful? Give feedback.
All reactions
-
For example. We are working on a little auth-library atm with keycloak integration.
There are no UI components in this lib. Like a login-form or something like that. These are handled by the Keycloak OIDC provider.
But there are services, which are keeping state, tokens, etc. And which has methods like login()
, logout()
etc.
What would be the best way to test them with (or without) testing-library. Since this might be hard(er)
The more your tests resemble the way your software is used,
the more confidence they can give you.
Or is this what you meant by:
creating the service manually via its constructor
In that case, the answer to the question "What would be a proper way to test (standalone) services" would be:
Use (default) Jest, without testing-library helpers. And just Inject / Or create the AuthService via it's constructor and setup/call the methods you need to test.
Correct?
Beta Was this translation helpful? Give feedback.
All reactions
-
Correct.
For most of the service tests that I've seen, you don't need to use the Angular TestBed or the Angular Testing Library..
You can create the service "manually" via its constructor.
There are a few exceptions when this is harder (e.g. when the service requires a lot of Angular services).
For your example, I would create a AuthService instance and provide a mock for the keycloak integration (if it's making network requests),
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 3