apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.38k stars 2.66k forks source link

MockedProvider throw error in just imported #7532

Closed devdany closed 3 years ago

devdany commented 3 years ago

I am trying mocking apollo provider for test (jest).

import React from 'react';
import { mount } from 'enzyme';
import { MockedProvider } from '@apollo/client/testing';
import MoonLoader from 'react-spinners/MoonLoader';
import Index, { DELETE_CLINIC } from './index';

const mocks = [
  {
    request: {
      query: DELETE_CLINIC,
      variables: {
        id: 1,
      },
    },
    result: {
      data: {
        id: 1,
      },
    },
  },
];
 it('products bringing..: It should work the Circular Progress', () => {
    const Wrapper = mount(
      <MockedProvider mocks={mocks} addTypename={false}>
        <Index loading />
      </MockedProvider>,
    );
    expect(Wrapper.find(MoonLoader)).toHaveLength(1);
  });

But, test is can not started because of error throwded in MockedProvider

 TypeError: Object prototype may only be an Object or null: undefined
        at setPrototypeOf (<anonymous>)

       5 | // import { Clinic } from 'types/graphql';
       6 | // import ClinicRow from 'components/molecules/ClinicRow';
    >  7 | import { MockedProvider } from '@apollo/client/testing';
         | ^
       8 | import TestRenderer from 'react-test-renderer';
       9 | import Index, { DELETE_CLINIC } from './index';
      10 |

      at Object.__extends (node_modules/@apollo/client/node_modules/tslib/tslib.js:69:9)
      at node_modules/@apollo/client/utilities/testing/mocking/MockedProvider.js:8:5
      at Object.<anonymous> (node_modules/@apollo/client/utilities/testing/mocking/MockedProvider.js:32:2)
      at Object.<anonymous> (components/organisms/ProductList/productList.spec.tsx:7:1)

What is the problem?

HeyParkerJ commented 3 years ago

@devdany I believe there is an undocumented backwards incompatibility between apollo client v2 and v3 within webpack and/or jest config that will cause this, due to some unknown bundling issues that have arisen since the v3 upgrade. I have attempted to document what I could in #7464.

I was able to fix this issue by not running my source code through either webpack's resolve as well as not running it through jest's moduleDirectories.

To clarify, this was my configuration before:

// webpack.config.js
resolve: {
  modules: ['node_modules', 'client'],
}

// jest.config.js
  moduleDirectories: ['node_modules', 'client'],

And I had to refactor my application and imports to support this config

// webpack.config.js

resolve: {
  modules: ['node_modules'],
}

// jest.config.js
  moduleDirectories: ['node_modules'],
hwillson commented 3 years ago

Let us know if this is still a concern with @apollo/client@latest - thanks!