-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Testing Error State of Mutation is throwing Global Error #7167
Comments
I'm experiencing the same problem with a useQuery. I'll just avoid testing errors for now and wait for a response on this thread. |
Experiencing the same thing. As mentioned in the previous thread, you can work around it by swallowing errors with a try/catch, but i hate adding extra code in my components JUST for the tests to work. I would assume the MockedProvider would know to swallow these errors itself, and expect the returned |
Same issue, different flavor. Trying to migrate into |
This comment managed to get around it using errorPolicy: <MockedProvider
mocks={mocks}
addTypename={false}
defaultOptions={{
mutate: {
errorPolicy: 'all'
}
}}
> You can then target to find the element rendered by |
@markcnunes Unfortunately this solution breaks the This seems like a high priority issue. |
Just got bitten by this ... wasted a couple of hours. If there's no appetite to change behaviour by the Apollo team, then this behaviour really needs to be highlighted in the documentation. I guess my expectation was any errors would be passed back in the Seems the best way to handle this is by adding a empty |
I've been struck with the same issue just now and while this seems obvious now I have to admit that documentation isn't clear about this, so here's what's happening: even though the That being said, in this example: <button onClick={() => mutate({ variables: { name: 'Buck' } })}>
Documentation while correct does not give an example that can pass tests. TLDR; in order to avoid your tests failing, you have to use the mutation in an |
I'm also experiencing this issue. |
+1 |
Thanks all, it seems that some commenters see this as a documentation issue and some see this as a source code issue. @thisfrontenddev what docs page would you recommend changing? |
For me, its a lack of documentation around the handling of errors in general for mutations (via useMutation) which is the issue. I had to go through a laborious trial/error trying to understand the scenarios in which a mutation would throw or call the
This is different to how queries work, where they don't throw at all and instead pass errors back either through the return value of a lazy query, or the error property of a greedy query. I would expect a lazy query and a mutation to behave in a similar way as they have a similar interface. It is different again if you use the |
Thanks for that response @leepowelldev! In 3.7.4 we did ship a patch to the Does that help clear things up? |
That just fixed the bug I reported that both sets of onError callbacks are called if both present. I still feel there are lots of inconsistencies on how mutations behave when dealing with errors - and although I realise it would be difficult to change that behaviour, I think the docs should be updated to reflect that behaviour much more clearly. On 14 Feb 2023, at 04:21, Jeff Auriemma ***@***.***> wrote:
Thanks for that response @leepowelldev! In 3.7.4 we did ship a patch to the useMutation callbacks: #10425
Does that help clear things up?
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: ***@***.***>
|
@bignimbus So I'm running into something similar, in which a mutation performed using |
Try throwing a GraphQL error in the results:
Straight from the docs: https://www.apollographql.com/docs/react/development-testing/testing/#graphql-errors. Probably wasn't there yet when the original question was posted. |
@jtonneyck there is actually a difference between the two properties. The top-level I agree though, we could have some better documentation around this. |
He @jerelmiller
I got an error back on:
Which is I believe the original motivation behind the question. It served my purpose at least. ;) |
Oh ya that will definitely work as well. I just wanted to point out that there is a difference between the 2 error properties and they aren't interchangeable since they impact the behavior in different ways. Glad this solution works well for you though! |
Got bite by the same problem, putting the params didn't solved the problem for testing error from network: defaultOptions={{
mutate: {
errorPolicy: 'all',
},
}} It works for GraphQLError sent in response tho. |
I want to reopen apollographql/react-apollo#2614 from the old repo as I ran into the exact same Issue today with
@apollo/client": "3.2.2
.I have a component that uses the useMutation hook and renders differently when useMutation returns an error
I want to Test this behaviour as described in the Docs
Intended outcome:
The Test should pass without throwing an error.
Actual outcome:
Passing mocks with an Error to
<MockedProvider />
does actually throw a global ErrorI figured out that when I add an
onError
option touseMutation
my test runs as expected but I guess this is just a workaround.How to reproduce the issue:
I cant share the code of the application I am working on but I can try to create a Codesandbox if neccessary but I hope my explanation is detailed enough.
Versions
System:
OS: macOS Mojave 10.14.6
Binaries:
Node: 10.16.0 - /usr/local/bin/node
npm: 6.11.2 - /usr/local/bin/npm
Browsers:
Chrome: 86.0.4240.80
Firefox: 81.0.1
Safari: 12.1.2
npmPackages:
@apollo/client: ^3.2.2 => 3.2.2
"jest": "^24.9.0",
The text was updated successfully, but these errors were encountered: