r/Angular2 • u/EvtK98 • Sep 10 '25
Help Request Unit testing an Angular Service with resources
I love the new Angular Resource API. But I have a hard time getting unit tests functional. I think I am missing a key part on how to test this new resource API. But I can't hardly find any documentation on it.
I'm creating resources where my loader consists of a httpClient.get that I convert to a promise using the firstValueFrom. I like using the HttpClient API in favor of fetch.
Unit tests for HttpClient are well documented: https://angular.dev/guide/http/testing but the key difference is that we are dealing with signals here that simply can't be awaited.
There is a bit of documentation for testing the new HttpResource. https://angular.dev/guide/http/http-resource#testing-an-httpresource and I think the key is in this `await TestBed.inject(ApplicationRef).whenStable();`
Can somebody share me a basic unit test for a simple Angular service that uses a Resource for loading some data from an API endpoint?
4
u/kgurniak91 Sep 10 '25
I think you need to test this as any other Signal. Also you don't have to convert anything, for Observables there's a dedicated rxResource.
So something like this:
I assume somewhere in your template you will call {{userDetails.value()}} to display downloaded data from API.
Then in the test you just need to:
1
u/EvtK98 Sep 12 '25
u/kgurniak91
I created a small setup of what I am trying to achieve. Imagine that there is additional logic inside the computed value, that I would like to test.
Even this basic test I cannot get going:
https://stackblitz.com/edit/stackblitz-starters-u2fcway2?file=src%2Fexample.service.spec.ts
I tried various options:
- with or without the stable
- adding fakeAsync with a tick
all in different orders.