Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Force test-utils to use the CJS version of React Apollo #2907

Merged
merged 1 commit into from
Mar 27, 2019
Merged

Conversation

hwillson
Copy link
Member

When running MockedProvider tests from a CLI or CI, those tests are run using the CommonJS bundled version of React Apollo (react-apollo.cjs.js). The test-utils themselves however, like MockedProvider, are not included in the production react-apollo.cjs.js bundle, to help keep the prod bundle size down. To use MockedProvider, it must be accessed using a nested/direct import like:

import { MockedProvider } from "react-apollo/test-utils";

Unfortunately, this nested import means MockedProvider is loaded outside of the react-apollo.cjs.js bundle, which means it requires and uses its own versions of other supporting React Apollo files. This causes problems in areas like using React's Context API. Both MockedProvider and the component's it's attempting to test create and use their own versions of shared Context, which are not available to each other. So when MockedProvider creates a new Context and adds the client instance to it, its child components don't have access to it, since they've created their own Context separately.

In React Apollo 3.0 we're going to move the test-utils out into their own package, which will fix this problem. As a workaround for now, this PR forces the test-utils to use the react-apollo.cjs.js version of React Apollo. This means tests have access to the same dependencies as the components being tested.

Fixes #2900.

@hwillson hwillson changed the title Issue 2900 Force test-utils to use the CJS version of React Apollo Mar 27, 2019
When running `MockedProvider` tests from a CLI or CI, those
tests are run using the CommonJS bundled version of React Apollo
(`react-apollo.cjs.js`). The `test-utils` themselves however, like
`MockedProvider`, are not included in the production
`react-apollo.cjs.js` bundle, to help keep the prod bundle size
down. To use `MockedProvider`, it must be accessed using a
nested/direct import like:

```
import { MockedProvider } from "react-apollo/test-utils";
```

Unfortunately, this nested import means `MockedProvider` is
loaded outside of the `react-apollo.cjs.js` bundle, which means
it requires and uses its own versions of other supporting
React Apollo files. This causes problems in areas like using
React's Context API. Both `MockedProvider` and the
component's it's attempting to test create and use their own
versions of shared Context, which are not available to each
other. So when `MockedProvider` creates a new Context and adds
the `client` instance to it, its child components don't have
access to it, since they've created their own Context
separately.

In React Apollo 3.0 we're going to move the `test-utils` out
into their own package, which will fix this problem. As a
workaround for now, this commit forces the `test-utils` to use
the `react-apollo.cjs.js` version of React Apollo. This means
tests have access to the same dependencies as the components
being tested.

Fixes #2900.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Could not find "client" in the context of ApolloConsumer when using react-apollo 2.5.3 with MockedProvider
1 participant