-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Signals : effect() is not triggered for unit tests #50466
Comments
Hi ! Effects are currently hooked onto the change detection system, if there is no change detection, they won't be fired. The same test on a component will be fine :
|
Hey, thank you for the information ! Does that mean effects can't be tested on services ? I'm not aware of change detection for those ... Would that also imply that services should avoid using effects ? It's relatively new to me, so I'll take any information I can find ! Edit : sorry this is the same person, just a different account on my phone |
Of course they can be tested when there is a component in the flow of the test :
In this use case, both effects are called because change detection happened (because of the component, as you assumed correctly). |
So I would have to create a stubbed component for every service with effects that I want to test ? To be honest I usually do unit testing without the testbed, by manually creating instances of my elements, I tried to get back to the testbed for the signals, but if I also have to add this step too, it seems a bit overkill to me ... |
Signals don't really make sense without components. They exist to bring fine grained reactivity to components, thus effect doesn't really make sens outside of Change Detection. If you're looking for traditionnal reactivity, RxJs is still your best horse ! |
In my (particular) case, the effect is listening on the Auth service to get the user, then makes an http request to update another state management service, that is used by components. I thought it would be a cool way of using signals to discover them ! But thank you for your input on that, I will indeed stay on rxjs and keep signals for my components. You were very helpful, thank you ! |
I am facing the same problem, but according to the RFC, Signals is never intended to be used only in components. From reading the effect implementation, I believe that if I can get an instance of https://github.com/angular/angular/blob/main/packages/core/src/render3/reactivity/effect.ts#L33 I think it would be worth looking into making |
Totally agreed @lacolaco! Meanwhile, if that can help someone here, here is how we dealt with this for |
With #51049 we would have a work arround by triggering a Microtask like with |
#51049 landed and it has a new |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
When unit testing an effect, it seems that it cannot be triggered. I would expect my effect to trigger so that I can test its content.
Here is a ZIP file of a simple project to test it : unzip it, run
npm install
, thennpx ng test
.Main files are
app.service.ts
andapp.service.spec.ts
, code is fairly simple you'll see.test.zip
Please provide a link to a minimal reproduction of the bug
Please see above
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run
ng version
)Anything else?
No response
The text was updated successfully, but these errors were encountered: