Unable to appropriately mock a SignalStore when its within the providers
array of the standalone component being tested
#4518
-
Which @ngrx/* package(s) are the source of the bug?signals Minimal reproduction of the bug/regression with instructionsgiven the following component and signal store:
and a test (similar test using spectator also fails) for this component which attempts to expect that the value of the field signal from the signal store is equal to a mock value provided in the spec (not the default value of "hello")
We can see that even though the test bed provides an instance of the AppComponentStore, the component is still getting its own created and favoring that over the one provided in the test bed. If we import the AppComponentStore from a module or the signal store is provided globally then mocking works as expected and the test passes. Expected behaviorWe would expect that the signal store would still be mocked appropriately even if the component has it referenced in its own Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)angular: ^18.2.0 Other informationI know the testing information is not quite ready yet for signal stores and i am unsure of if this is an angular issue or a signal store issue but figured i would post here first and get some feedback. I dont really want to have to import a module which provides the signal store or make my signal store global just to be able to test it appropriately. Thanks for all the hard work, signal stores are really cool I would be willing to submit a PR to fix this issue
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
@skyleguy, for overriding Services of a component, you need to use TestBed.overrideComponent(AppComponent, {
set: {
providers: [
{
provide: AppComponentStore,
useValue: {
field: signal('hi'),
},
},
],
},
});
TestBed.configureTestingModule(...) Let me know if that works for you. |
Beta Was this translation helpful? Give feedback.
-
yes thank you, a little newer to standalone components still in my area and did not realize there was a difference in testing for standalone components. thanks for pointing that out. for my friends using spectator it would look something like this:
and then if you need to get that instance you do |
Beta Was this translation helpful? Give feedback.
@skyleguy, for overriding Services of a component, you need to use
TestBed.overrideComponent
:Let me know if that works for you.