Cannot directly mount `Context.Consumer` without console error
See original GitHub issueIf Context
is the result of React.createContext
(React 16.3+), then
it appears that mount
can handle Context.Consumer
s that appear in
the tree, but raises a console error if the root itself is a consumer
(despite appearing to otherwise behave correctly).
The same error occurs with the v16.3 adapter or the v16 adapter.
The error message is:
Warning: Failed prop type: Invalid prop `Component` supplied to `WrapperComponent`.
in WrapperComponent
Here is a repro:
const React = require("react");
const Enzyme = require("enzyme");
const Adapter = require("enzyme-adapter-react-16.3");
Enzyme.configure({ adapter: new Adapter() });
const ColorContext = React.createContext("yellow");
beforeEach(() => {
console.warn = jest.fn();
console.error = jest.fn();
});
afterEach(() => {
expect(console.warn).not.toHaveBeenCalled();
expect(console.error).not.toHaveBeenCalled();
});
// This passes.
it("should mount a component that uses context", () => {
class Demo extends React.Component {
render() {
return React.createElement(ColorContext.Consumer, {}, color =>
React.createElement("span", {}, `${color} is a good color`)
);
}
}
const e = Enzyme.mount(React.createElement(Demo));
expect(e.html()).toEqual("<span>yellow is a good color</span>");
});
// This fails.
it("should mount the consumer directly", () => {
const e = Enzyme.mount(
React.createElement(ColorContext.Consumer, {}, color =>
React.createElement("span", {}, `${color} is a good color`)
)
);
expect(e.html()).toEqual("<span>yellow is a good color</span>");
});
Reproduce by running yarn jest
with the following package.json
:
{
"license": "MIT",
"dependencies": {
"enzyme": "3.4.1",
"enzyme-adapter-react-16.3": "1.0.0",
"jest": "23.5.0",
"prettier": "^1.14.2",
"react": "16.3.2",
"react-dom": "16.3.3"
}
}
Versions: node v8.9.4, npm v5.6.0, yarn v1.7.0, Mint 18.2 (Ubuntu 18.04)
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Cannot directly mount `Context.Consumer` without console error
If Context is the result of React.createContext (React 16.3+), then it appears that mount can handle Context.Consumer s that appear in
Read more >Invariant Violation: Could not find "store" in either the context ...
It's pretty simple. You're trying to test the wrapper component generated by calling connect()(MyPlainComponent) .
Read more >How to use React Context like a pro - Devtrium
In the Page component, we are accessing the context by using the useContext hook directly. But what if the component is not actually...
Read more >React Context for Beginners – The Complete Guide (2021)
In this comprehensive guide, we will cover what React context is, how to use it, when and when not to use context, and...
Read more >Strict Mode - React
Strict mode checks are run in development mode only; they do not impact the production ... Detecting unexpected side effects; Detecting legacy context...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The original repro was correct. However,
enzyme-adapter-react-16.3
depends onenzyme-adapter-utils@^1.5.0
. When I created the original repro, this resolved to version 1.5.0. It now resolves to version 1.6.0, which fixes the problem.The following
package.json
explicitly pins the subdependency, and enables reproducing the original failure (with the same test script above). Again, put this into yourpackage.json
, put the test file in (say)test.js
, and runyarn && yarn jest
:(Edited to remove valid but unnecessary dependency in
package.json
.)The fact that the test passes on latest versions (without the explicitly pinned subdependency) does indeed suggest to me that the issue has been resolved. Thanks for the fix!
We don’t yet have proper support for React’s new context. Thanks for the report.