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.

Cannot directly mount `Context.Consumer` without console error

See original GitHub issue

If Context is the result of React.createContext (React 16.3+), then it appears that mount can handle Context.Consumers 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:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
wchargincommented, Aug 28, 2018

The original repro was correct. However, enzyme-adapter-react-16.3 depends on enzyme-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 your package.json, put the test file in (say) test.js, and run yarn && yarn jest:

{
  "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"
  },
  "resolutions": {
    "enzyme-adapter-utils": "1.5.0"
  }
}

(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!

1reaction
ljharbcommented, Aug 14, 2018

We don’t yet have proper support for React’s new context. Thanks for the report.

Read more comments on GitHub >

github_iconTop 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 >

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