Skip to content
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

MockedProvider - mutation errors are not passed to the component #4283

Closed
albertortizn26 opened this issue Jan 4, 2019 · 9 comments
Closed

Comments

@albertortizn26
Copy link

albertortizn26 commented Jan 4, 2019

Hi,
I have been facing a problem mocking data with MockedProvider.
With queries all works as expected, when I mock success and errors, the problem comes with mutations, success are ok but when I try to mock an error it is not passed to my component in the render function, instead an Error is thrown.

Test:

 const mocks = [
        {
            request: {
                query: MY_MUTATION,
                variables: {
                    userId: 'error'
                },
            },
            error: new Error('boom!')
        }
];

it('my test that should pass', () => {

        const wrapper = mount(
            <MockedProvider mocks={mocks}>
                <MyComponent ... />
            </MockedProvider>
        );
...

Component:

 <Mutation mutation={MY_MUTATION} >
                {(cancelMandate, {loading, error}) => (
...

When I run the test it just throw the exception but in a live environment it is passed to the component!

@albertortizn26 albertortizn26 changed the title MockedProvider - mutation errors are thrown MockedProvider - mutation errors are not passed to the component Jan 4, 2019
@CarloPalinckx
Copy link
Contributor

What versions are you using?

@royderks
Copy link

royderks commented Jan 25, 2019

Having the same problem, with "apollo-boost": "0.1.7", passing something like

const mocks = [{
      request: {
        query: ADD_OFFER_MUTATION,
        variables: offerMock
      },
      result: {
        errors: [{ message: "Error!" }],
      },
    }];

to the MockedProvider, my console returns throw err; ^ Error: GraphQL error: Error!.

If I pass the same for a Query, it works just fine btw..

@mintyfloss
Copy link

Same with version 2.5.1

@veddermatic
Copy link

I see the same, but in queries as well. Both of these:

{
  request: {
    query: MY_QUERY,
    variables: myVars
  },
  result: {
     errors: [new GraphQLError("Error!")],
  }
};

and

{
    request: {
      query: MY_QUERY,
      variables: myVars,
    },
    error: new Error('aw shucks'),
  };

Simply throw an error in Jest and fail the test. Using:

"@apollo/react-testing": "3.0.1",
"apollo-boost": "0.4.4",
"react-apollo": "3.0.1",

@svalchinov
Copy link

I'm experiencing the same, what's the workaround if a fix is missing?

@svalchinov
Copy link

svalchinov commented Jan 17, 2020

We managed to get around it using errorPolicy if anyone else gets stuck:

<MockedProvider mocks={[mocks]} addTypename={false} defaultOptions={{
    mutate: {
        errorPolicy: "all"
    }
}}>
    <Component />
</MockedProvider>

@WillSquire
Copy link

catch on the mutation's promise helped add a work around for me. Might be related to #3876?

@MitchK
Copy link

MitchK commented Jan 21, 2021

We ran into the same issue and the error is no longer thrown if we handle the error using an onError handler:

const [mutate, {loading, error}] = useMutation(
  MY_GQL_MUTATION,
  {
    variables: ...
  },
  onError: ...
);

@hwillson
Copy link
Member

We are planning a significant MockedProvider overhaul in the not too distant future. In the meantime, there are several workarounds listed in this issue, so I'll close it for now. Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants