<MockedProvider> Mocks queries
Getting error:
Invariant Violation: Could not find "client" in the context or passed in as an option. Wrap the root component in an <ApolloProvider>, or pass an ApolloClient instance in via options.
Use the MockedProvider from version 3.1.1
If you say that it works for you I will try to create reproduction, but this error pretty much appeared in 3 projects when upgraded react-testing to 3.1.1. Downgrade to 3.1.0 fixed it.
3.1.1
3.1.0 works as expected
System:
OS: macOS 10.14.6
Binaries:
Node: 12.8.0 - /usr/local/bin/node
Yarn: 1.17.3 - ~/.yarn/bin/yarn
npm: 6.10.2 - /usr/local/bin/npm
Browsers:
Chrome: 76.0.3809.132
Firefox: 69.0
Safari: 12.1.2
npmPackages:
apollo-cache-inmemory: ^1.6.3 => 1.6.3
apollo-client: ^2.6.4 => 2.6.4
I am getting the same when upgrading react-apollo from 3.1.0 to 3.1.1
I noticed this as well, but upgrading both react-apollo and @apollo/testing-library to 3.1.1 seems to have fixed it.
Make sure you update @apollo/react-common first. You might even not have installed it, but it was installed as a sub dependency of e.g. @apollo/react-hooks. If your @apollo/react-common is at 3.0.1 and you update @apollo/react-testing to the latest of 3.1.1 you will get this:
diff --git a/package-lock.json b/package-lock.json
index fdd3c03..9eb7b3f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -25,14 +25,26 @@
}
},
"@apollo/react-testing": {
- "version": "3.0.1",
- "resolved": "https://registry.npmjs.org/@apollo/react-testing/-/react-testing-3.0.1.tgz",
- "integrity": "sha512-omcKnWCOpyTcg92GO4q2pb5t/a0RKP3iHe6mZ8fwkT/n3vAJ/wGuOz+MAOCQlZWvDYC4zKFHu1Se6X8ZaV8jjg==",
+ "version": "3.1.1",
+ "resolved": "https://registry.npmjs.org/@apollo/react-testing/-/react-testing-3.1.1.tgz",
+ "integrity": "sha512-tjZ5zQ8WRSu4gXCfhNWDt3rgsunFZ7QapyDNp9ykVIuC/JrusUdbGieq9PajZsmLZUMOfQUEEoWBNRWNAsXlJw==",
"dev": true,
"requires": {
- "@apollo/react-common": "^3.0.1",
+ "@apollo/react-common": "^3.1.1",
"fast-json-stable-stringify": "^2.0.0",
"tslib": "^1.10.0"
+ },
+ "dependencies": {
+ "@apollo/react-common": {
+ "version": "3.1.1",
+ "resolved": "https://registry.npmjs.org/@apollo/react-common/-/react-common-3.1.1.tgz",
+ "integrity": "sha512-lMiczOm4sD16eI9Ai+AUY+Jl3C/FNm4IpoZiAbe35TqMuD7HpZ4OJmXAbT3CzfrAqsKK8+rHgRvTiPI2kQjIWg==",
+ "dev": true,
+ "requires": {
+ "ts-invariant": "^0.4.4",
+ "tslib": "^1.10.0"
+ }
+ }
}
},
"@babel/code-frame": {
diff --git a/package.json b/package.json
index 9599825..236ee0b 100644
--- a/package.json
+++ b/package.json
@@ -58,7 +58,7 @@
"upgrade-version": "./scripts/upgrade-version.sh"
},
"devDependencies": {
- "@apollo/react-testing": "^3.0.1",
+ "@apollo/react-testing": "^3.1.1",
"@babel/core": "^7.5.5",
"@babel/polyfill": "^7.6.0",
"@babel/preset-env": "^7.5.5",
See how now @apollo/react-common was added as a dependency of @apollo/react-testing ? If it was updated before, that would not be the case.
So the problem arises when we have two different @apollo/react-common dependencies, one in global node_modules, and one inside @apollo/react-testing local node_modules as in the above diff. At this point, when you import MockedProvider it uses different context, than e.g. useQuery, because they refer to different context instance.
Most helpful comment
Make sure you update
@apollo/react-commonfirst. You might even not have installed it, but it was installed as a sub dependency of e.g.@apollo/react-hooks. If your@apollo/react-commonis at 3.0.1 and you update@apollo/react-testingto the latest of 3.1.1 you will get this:See how now
@apollo/react-commonwas added as a dependency of@apollo/react-testing? If it was updated before, that would not be the case.So the problem arises when we have two different
@apollo/react-commondependencies, one in global node_modules, and one inside@apollo/react-testinglocal node_modules as in the above diff. At this point, when you importMockedProviderit uses different context, than e.g.useQuery, because they refer to different context instance.