react: "Missing React element for debugID" warning when testing components with Jest
Do you want to request a feature or report a bug?
Report a bug.
What is the current behavior?
When testing components, Warning: ReactComponentTreeDevtool: Missing React element for debugID 1 when building stack is shown when calling TestUtils.renderIntoDocument.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/reactjs/69z2wepo/).
https://github.com/angusfretwell/reproduce-debugid-warning (npm install && npm test).
What is the expected behavior?
According to #7187 this is an internal warning and shouldn’t be displayed.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
- react@15.2.1
- react-addons-test-utils@15.2.1
- react-dom@15.2.1
I didn’t receive this warning on 15.2.0.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 11
- Comments: 62 (8 by maintainers)
Commits related to this issue
- Eagerly evaluate inline requires in Jest (#7245) * Eagerly evaluate inline requires in Jest I inlined some requires in #7188 to fix the build size regression. However this caused an issue with Je... — committed to facebook/react by gaearon 8 years ago
- Eagerly evaluate inline requires in Jest (#7245) * Eagerly evaluate inline requires in Jest I inlined some requires in #7188 to fix the build size regression. However this caused an issue with Jest ... — committed to facebook/react by gaearon 8 years ago
- Chapter v0.2.0. diff --git a/404.html b/404.html new file mode 100644 index 0000000..5fd4569 --- /dev/null +++ b/404.html @@ -0,0 +1,4 @@ +--- +permalink: /404.html +--- +{% include_relative index.ht... — committed to n6g7/chapter by deleted user 7 years ago
Let’s keep it open until either fix gets in.
OK, I started looking into it in #7395. This is not a super high priority since it’s just an errant warning and it doesn’t fail your tests. Still, we’d like to get it fixed of course.
Oh, I see what is happening here now.
Here is how Jest works in open source:
jest.resetModuleRegistry()implicitly in everybeforeEachcall. When doingrequire('React')(or import) on the top-level and thenrequire('React')inside of a call toit), they will be fresh copies both times. I suspect the debug module is freshly required from an old version of React which will then go and require new versions of modules it requires itself.At Facebook here is how Jest works:
jest.resetModuleRegistry()inbeforeEach. Not every test does this, but every test should and I’ve been hoping to turn the open source behavior on internally as well.const React = require('React')will be removed and inlined into the code. Together with the module registry reset, this works pretty well and every call toitis completely isolated. Previously, the way we required modules in our tests internally was always quite the mess.What we do at FB is what I’d recommend most people to do however people in open source have decided they want to use ES2015 imports, even in tests and there is not much I can do to stop them. That way they might end up with this confused state.
Jest sets the
NODE_ENVtotest– could we leverage this in React?@jessy1092 What versions are you running?
I’m still seeing multiple instances of
ERROR: 'Warning: ReactComponentTreeDevtool: Missing React element for debugID 17 when building stack'react 15.3.0 react-dom 15.3.0
karma 1.1.2 karma-mocha 1.1.1 karma-mocha-reporter 2.0.5 mocha 2.5.3
Node 5.8.0 npm 3.8.2 Mac OS 10.11.6
Repro steps:
Clone, build and test mxstbr/react-boilerplate
git clone https://github.com/mxstbr/react-boilerplate.git && cd react-boilerplatenpm run setupnpm testAll (78) tests pass. Errors seem to appear during
<FeaturePage />tests Testing FeaturePage only (npm run test -- --grep FeaturePage) does not produce errors.ract-addons-test-utils is not included in the react-boilerplate project, but adding it does not remove the errors.
@gaearon I test on official release 15.3.0. The warning was gone. Thanks for hard work. 💯
As a super temporary fix you can put
require('react/lib/ReactComponentTreeDevtool');at the top level in your tests. I think this should fix the issue. Be sure to remove it when the next version is out 😄 . And only do this in tests.Could this be some sort of a Jest issue (related to mocking?). In your example, if I put a log in the top level scope of
ReactComponentTreeDevtool.js, I see it twice.I’m not sure if this is normal.
I can also “fix” the issue by moving
requires inside the test:I would advise you to file an issue with Jest. It shouldn’t be executing our modules twice.
react@15.3.0 jest-cli@14.1.0Can you try sticking
"persistModuleRegistryBetweenSpecs": trueinto your config and whether that makes these warnings go away? We are gonna ship with this as a default in the next major of Jest and it is annoying that we haven’t done this before.Jest actually resets all the modules in between calls to
itto give even more isolation. So doing something like:both “sum” modules will actually be different instantiations of the module. So if you don’t want to change the option above, you can also inline all your requires manually:
it(‘works’, () => { const React = require(‘React’); expect(<div />).<matcher>; });
When we persist the module registry, both of them will be the same (
===is true). The downside is that you might have module local state between tests that is carried over. I think since people decided to predominantly useimportstatements in new code, this option makes no sense by default any more.Face. Palm. Apparently I need more coffee.
Routing ERR to /dev/null works perfectly, as it should. Thanks again.
Along the same lines, you could also do something like
npm run test 2>/dev/nullif you are in a unix environment.