question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MockedProvider support?

See original GitHub issue

react-apollo exports react-apollo/test-utils#MockedProvider, I can’t find anything similar for react-apollo-hooks.

I could use

jest.mock('react-apollo-hooks', () => ({
  __esModule: true,
  useQuery: () => ({ data: mockedData, loading: false }),
})

Is this supported or is jest.mock the suggested approach?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
FezVrastacommented, Mar 25, 2019

Thanks for the reply!

Would it make sense to expose a pre-built MockedProvider that internally makes use of apollo-link-mock and apollo-cache-inmemory so that this library exposes a similar API surface as react-apollo?

5reactions
kusmierzcommented, Jul 10, 2019
import React from 'react';
import { ApolloConsumer } from 'react-apollo';
import { MockedProvider, MockedProviderProps } from 'react-apollo/test-utils';
import { ApolloProvider as ApolloHooksProvider } from 'react-apollo-hooks';

// this adds react-apollo-hooks support for react-apollo's MockedProvider
const MockedHooksProvider = ({ children, ...props }: MockedProviderProps) => (
  <MockedProvider {...props}>
    <ApolloConsumer>
      {(client) => (
        <ApolloHooksProvider client={client}>
          {children}
        </ApolloHooksProvider>
      )}
    </ApolloConsumer>
  </MockedProvider>
);

export default MockedHooksProvider;

you could also use above’s <MockedHooksProvider /> instead of react-apollo’s <MockedProvider /> as above. Then use <MockedHooksProvider /> in the same way as <MockedProvider />:

it('renders without error', () => {
  renderer.create(
    <MockedHooksProvider mocks={mocks} addTypename={false}>
      <Dog name="Buck" />
    </MockedHooksProvider>,
  );
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing React components - Apollo GraphQL Docs
The MockedProvider component enables you to define mock responses for individual queries that are executed in your test. This means your test doesn't...
Read more >
MockedProvider can't mock queries with client directives #3316
When using a MockedProvider and a query with client directives, the client directives should resolve. Query example: query Cats { cats { name...
Read more >
Debugging Apollo GraphQL MockedProvider - Swarmia
This blog post is here to help! ... Let's go through five practical tips to make debugging Apollo MockedProvider more enjoyable.
Read more >
The Do's and Don'ts of Testing Apollo in React - Adam Hannigan
By mocking requests at the service worker level you enable your ... The Apollo MockedProvider outlines that “Your test must execute an ...
Read more >
Testing React components - Apollo GraphQL Docs
... functionality with Apollo Client itself, so they will be supported long-term. ... dog.test.js import { MockedProvider } from '@apollo/client/testing'; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found