Open sbarfurth opened 4 months ago
@sbarfurth maybe that the v13 migration guide isn't really clear/specific about this.
Destroying the TestBed
has little to do with resetting the cached value of a selector.
To reset the selectors you must use resetSelectors
.
getTestBed().inject(MockStore, null).resetSelectors();
The problem occurs when relying on Angular to teardown the Store.
While doing so, resetSelectors
loses its context and cannot reset previously set selectors.
Yes, I realize this. However, the migration guide suggests that the behavior is somehow different or a non-issue when using destroyAfterEach
. It seems to me that simply isn't true? As far as I can tell this has no bearing on whether or not you need to reset selectors. I'm only asking if it wouldn't make sense to update the migration guide since I think the current version is unclear at best. It might even be incorrect.
Which @ngrx/* package(s) are the source of the bug?
store
Minimal reproduction of the bug/regression with instructions
Reproduction in sbarfurth/selector-reset-repro.
Run
ng test
to reproduce.Expected behavior
All tests pass.
Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)
NgRx: v18.0.1 Angular: v18.0.7 Node: v20.15.0 Browser: Chrome v126 OS: macOS Sonoma 14.5
Other information
The automagic reset of the
MockStore
was removed with v13 as a breaking change. The migration guide states that this is a non-issue for tests usingdestroyAfterEach
: https://ngrx.io/guide/migration/v13#testing-reset-mock-storeThe
overrideSelector
method ofMockStore
sets the value on the memoized selector itself usingsetResult
when not using a string. This values will leak between tests regardless of whether theTestBed
is torn down or not since the memoized selector is only imported once per test suite.This can be remedied by manually calling
resetSelectors
on theMockStore
since that callsrelease
andclearResult
for every selector.It seems the migration guide is incorrect and that this implication of
overrideSelector
is undocumented.I would be willing to submit a PR to fix this issue