-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Change in _owner causes issues with shallow rendered tests #5292
Comments
I've been seeing some similar issues with shallow rendering. My current (messy - sorry!) workaround is this: // in ReactTestUtils.js
var NoopInternalComponent = function (element) {
var type = element.type.name || element.type;
var children = NoopInternalChildren(element.props.children);
var props = assign({}, element.props, { children: children })
this._renderedOutput = assign({}, element, { type, props });
this._currentElement = element;
};
var NoopInternalChildren = function (children) {
if (Array.isArray(children)) {
return children.map(NoopInternalChild);
} else if (children === Object(children)) {
return NoopInternalChild(children);
}
return children;
};
var NoopInternalChild = function (child) {
var props = child.props && child.props.children ? assign({}, child.props, {
children: NoopInternalChildren(child.props.children)
}) : child.props;
return assign({}, child, { _owner: null }, { props });
}; Let me know if that helps! My version currently breaks some of the Jest tests, but I'm happy to work on this some more and submit a PR. |
Thank you very much for this, it's been a tremendous help! While this solved the problem for my test case above it still failed on a large number of our more complex tests. I've been iterating on it and once I have all of our tests passing I'll add the updated version. |
Here is the updated workaround, it works for our 238 tests of varying complexity but I'd be interested to see if it passes all of your tests as well. var NoopInternalComponent = function (element) {
var type = element.type.name || element.type;
var props = {};
if (element.props.children) {
props.children = NoopInternalChildren(element.props.children);
}
var props = assign({}, element.props, props);
this._renderedOutput = assign({}, element, { type: type, props: props });
this._currentElement = element;
};
var NoopInternalChildren = function (children) {
if (Array.isArray(children)) {
return children.map(NoopInternalChild);
} else if (children === Object(children)) {
return NoopInternalChild(children);
}
return children;
};
var NoopInternalChild = function (child) {
if (child === null || child === undefined) { return; }
if (typeof child === 'string') { return child; }
if (Array.isArray(child)) {
return child.map(NoopInternalChild);
}
var props = child.props && child.props.children ? assign({}, child.props, {
children: NoopInternalChildren(child.props.children)
}) : child.props;
return assign({}, child, { _owner: null }, { props: props });
}; |
Awesome! If you don't mind trying, what happens if you do // NoopInternalChildren
if (Array.isArray(children) {
return children.map(NoopInternalChildren);
} and then use the earlier |
Much cleaner thanks, I guess I was in my head too much :) var NoopInternalComponent = function (element) {
var type = element.type.name || element.type;
var props = {};
if (element.props.children) {
props.children = NoopInternalChildren(element.props.children);
}
var props = assign({}, element.props, props);
this._renderedOutput = assign({}, element, { type: type, props: props });
this._currentElement = element;
};
var NoopInternalChildren = function (children) {
if (Array.isArray(children)) {
return children.map(NoopInternalChildren);
} else if (children === Object(children)) {
return NoopInternalChild(children);
}
return children;
};
var NoopInternalChild = function (child) {
var props = child.props && child.props.children ? assign({}, child.props, {
children: NoopInternalChildren(child.props.children)
}) : child.props;
return assign({}, child, { _owner: null }, { props: props });
}; |
I prefer applying a monkey patch, until we have this fixed in react by wrapping TestUtils in my own module:
https://gist.github.com/vfil/81049b1590aa3b9e439188a49a5d67bc |
This should be fixed by #6362. |
There appears to have been a change in how
_owner
is generated between 0.13 and 0.14 which is causing a number of our shallow rendered tests to fail when upgrading because the deepEquals fails on the_owner
property. Here is a simple example component which exhibits the issue.The text was updated successfully, but these errors were encountered: