-
Notifications
You must be signed in to change notification settings - Fork 48.4k
Refs with react-test-renderer (TypeError: Cannot read property 'ownerDocument' of null) #9244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think it's a intended behavior, see #7371 . Try enzyme. :) |
No, #7371 is unrelated. #7371 is about using The behavior is intentional but there's a workaround. You need to figure out which DOM nodes the library you're using expects, and mock them to the extent you care about (i.e. enough to make test pass). You can read about mocking refs for snapshot testing here. Hope this helps! |
Hello! Thanks @gaearon for your quick answer. Then, if I'm not confused, not only I must check my For instance, in my case: Then I must check the sources of For example, the file InkTabBarMixin have:
If I'm not confused I must make a Don't get me wrong, I'm a strong advocate or React & Jest, but I think this is getting seriously complicated when I just want to snapshot my component... It forces me to analyze not only my code, besides, the code of the packages, and the packages of the packages! Probably I'm confused and there is a better way to avoid this. Thanks for your patiente @gaearon , @yesmeck & @thymikee. Best regards!!! |
Well,... I'm sure that I won't get a solution, but at least I'm waiting for an answer 😢 Please, one last question, if I want to generate snapshots with Jest, then I must look for all the Thanks in advance. |
@aaronplanell See my comment. |
I don’t mean that you need to literally check every ref. I mean that you can write a snapshot test, and if it fails, look at which methods are expected on refs, and mock them. I don’t think you’ll find more than a few such places.
You don’t need to hardcode If you use Jest, you can also mock any components, e.g. jest.mock('third-party-library/ComponentThatReallyReliesOnDom', () => {
return (props) => <div>{props.children}</div>; // Just pretend it renders children
}); This would let you “skip” components that are too coupled to the DOM. |
Thanks @gaearon for your answer! I wrote:
And then I found:
I think I found another stone in my path 😢 If I am not confussed, working with Thanks all of you for your help. Best regards. |
Yes. But you can mock it too. jest.mock('react-dom', () => ({
findDOMNode() { return {} /* or whatever */; },
// if your tree depends on more methods add stubs here
})); |
Perfect!!! I wrote: jest.mock('rc-tabs/lib/ScrollableInkTabBar.js', () => {
return (props) => <div>{props.children}</div>;
});
jest.mock('react-dom', () => ({
findDOMNode() { return null; },
render() { return null; }
})); As you said and now I have this snapshot: <div
className="App">
<div
className="ant-tabs ant-tabs-top ant-tabs-line"
style={Object {}}>
<div />
<div
className="ant-tabs-content ant-tabs-content-animated"
style={
Object {
"marginLeft": "0%",
}
}>
<div
aria-hidden="false"
className="ant-tabs-tabpane ant-tabs-tabpane-active"
role="tabpanel"
style={undefined}>
Content of Tab Pane 1
</div>
<div
aria-hidden="true"
className="ant-tabs-tabpane ant-tabs-tabpane-inactive"
role="tabpanel"
style={undefined} />
<div
aria-hidden="true"
className="ant-tabs-tabpane ant-tabs-tabpane-inactive"
role="tabpanel"
style={undefined} />
</div>
</div>
</div> Now I can check that I have three tabas and make other snapshots for their children. Thanks a lot @gaearon 👍 |
Glad it helped! |
I think a far easier option would to be use Enzyme instead of react-test-renderer. Mocking can get out of hand pretty quickly if you're working on any libs that have a lot of components - (e.g. any material design based ones). |
I faced the same issue when write test for grafana plugin. Based on previous suggestion, I solve the issue with following mock: jest.mock('@grafana/ui', () => ({
...jest.requireActual('@grafana/ui'),
Tooltip(props:any) { return <div>{props.children}</div> },
})); |
Do you want to request a feature or report a bug?
A bug in snapshots.
What is the current behavior?
It seems that there is a problem with the snapshots when you use other libraries like
antd
. The snapshot launch:If I mock all the library, the snapshot is almost empty.
If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can
yarn install
andyarn test
.You can download this example of my Github account: https://github.com/aaronplanell/ant-design-issue_5428
In the
README.md
you will find, step by step, the full proccess.Besides, I opened an issue to
antd
: ant-design/ant-design#5428 and @yesmeck said me that was a problem of Jest/react-test-renderer.Then I opened an issue to
jest
: jestjs/jest#3195. This time @thymikee confirmed what @yesmeck said, it's a problem of react-test-renderer.What is the expected behavior?
I expect that the snapshot have the full HTML code in JSON format. It has no sense that snapshots fail if a library that you use (and works!) use
refs
.Please provide your exact configuration and mention your Jest, node, yarn/npm version and operating system.
I use this dependencies:
With this OS: Ubuntu 16.04
The text was updated successfully, but these errors were encountered: