Weird bug with Angular unit tests #10046
Describe the bug#
In the docs it says that in the unit tests we should await fixture.whenStable() so a query completes and we get its resulted data. It is true but if I add a class field that references to query.data the query's data becomes undefined.
Your minimal, reproducible example#
https://github.com/ms-dosx86/test-when-stable
Steps to reproduce#
- Run
ng test - See that the test passes
- Uncomment the commented line in
app.ts - Run the test again
- Test fails
Expected behavior#
I'm not sure what's going on there but I have a feeling that creating a class field that references to query.data shouldn't cause such behaviour.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- MacOS 26.0.1 (25A362)
- Chrome 143.0.7499.193
Tanstack Query adapter#
angular-query
TanStack Query version#
5.90.22
TypeScript version#
5.8.2
Additional context#
No response
TkDodo added labelspackage: angular-query
@ms-dosx86 your issue looks related to https://github.com/TanStack/query/issues/7488
The current implementation of the proxy of query does not work, so for now you can do this instead
app = () => this.appQuery.data();There, the proxy attribute accessor does not run on component initialization, fixing the issue.
@eliellis from my tests, it is a timing issue of the adapter. Some reproductions of those testing bugs can be fixed by doing
fixture.detectChanges() // Sometimes, appRef.whenStable() is called just before it knwons it's unstable, // so test can wait for js pending tasks to complete before with setTimeout // await new Promise(resolve => setTimeout(resolve, 0)); // await new Promise(resolve => setTimeout(resolve, 0)); const appRef = TestBed.inject(ApplicationRef); await appRef.whenStable();benjavicente(GitHub profile, opens in a new tab) assignedbenjavicente
This is fixed in
@tanstack/angular-query@rcnpm install @tanstack/angular-query@rc npx ng generate @tanstack/angular-query:migrate-from-experimental --dry-runIn the reproduction, I had to do a small change for it to work, required in Zone.js tests:
- fixture.detectChanges(); + fixture.autoDetectChanges();TkDodo closed this
Sign in to join this conversation.
I'm also having serious issues getting unit tests to work properly. There are a few other open issues that seem related to this:
If anyone has any insight–or has working examples of UTs using
jestas the testing framework, I'd love to see them!