Skip to content
Source
Sign inSign up
TanStack/query

Can't test Query mutations using Angular's HttpTestingController #7080

Send to my agent
ClosedTimebutt(GitHub profile, opens in a new tab) opened this issue 4 comments

Describe the bug

I have implemented a few mutations in an Angular application I am working on, but noticed during testing that using Angular's own HttpTestingController does not allow me to properly test said mutations.

I have investigated extensively, but can not get my tests to run correctly because of what seems to be a bug in Query itself.

Your minimal, reproducible example

https://github.com/Timebutt/query-angular-mutation-testing

Steps to reproduce

Clone above repository, run npm install and then run npm run test to run the reproduction example. The test will fail, even though from the way I have set up the test I'm expecting it to pass.

Notice that I have patched @angular/common to clearly indicate the difference between the GET and POST request in the HttpTestingController internals.. In the case of the GET request, the request correctly gets added to the open array before the match takes place. In the POST case, the match happens first, only then does the request get added in the array. This is why the test clearly fails.

I'm not entirely sure what might be causing this yet in Query internally, but I think something similar to a TestBed.flushEffects() might be needed to make sure that everything runs correctly in unit tests. I'm not sure if the ProxySignal makes things complicated here, then again: I'm not really at home in your codebase yet.

Expected behavior

I expect this test to pass. I dove into Angular source code and discovered the XHR request correctly gets scheduled in the internals of HttpTestingController. The odd thing is that the check for the specific XHR request happens after (!) adding it into the open array (see source) that holds all currently incomplete XHR requests in HttpTestingController internally.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Unrelated, ran this on my Windows and Mac

Tanstack Query adapter

angular-query

TanStack Query version

5.26.3

TypeScript version

5.3.2

Additional context

No response

  1. Does it work with await fixture.whenStable()?

    E.g.

    mutation.mutateAsync()
    await fixture.whenStable()
    controller.expectOne(..)
  2. Interesting, that does indeed fix it!

    Great news, but it feels like updated code examples that show how to test mutations would be very beneficial: a few of my colleagues at work already ran into this exact same issue and just abandoned or worked around the issue. I had a quick look, but I didn't immediately find any guidelines on testing in the documentation, am I missing the docs or are there simply none yet? I could rewrite and upgrade my example to be the start of the documentation for instance.

  3. arnoud-dv(GitHub profile, opens in a new tab) added labelspackage: angular-query

  4. arnoud-dv(GitHub profile, opens in a new tab) added labelsdocumentation

  5. I think the conclusion is there aren't any known issues at the moment but we could use a unit testing guide. If somebody wants to contribute such a guide that would be great 👍

    If writing a full guide would be too much, sharing your experiences here on Angular unit testing w. TanStack Query would also be appreciated ❤️ That would be helpful for anybody writing this unit testing guide.

  6. Closing this as it’s quite stale and there doesn’t seem to be a known issue here.

  7. TkDodo closed this

Sign in to join this conversation.