artofatih artofatih - 3 months ago 44
React JSX Question

TestUtils.renderIntoDocument return null, if it declared as anonymous function

I have 2 variants of code:

FIRST (declare as class):

export default class COMPONENT_NAME extends React.Component{
constructor(props){
super(props);
this.props = props;
}
....
render = () => <div className="clName"></div>
}


SECOND (declare as anonymous function):

export default (props) => <div className="clName"></div>


JEST CODE:

jest.dontMock('../js/components/COMPONENT_NAME/index');
const COMPONENT_NAME = require('../js/components/COMPONENT_NAME/index.js').default;
var loaderComponent;
...
...
function renderComponent() {
loaderComponent = TestUtils.renderIntoDocument(
<COMPONENT_NAME />
);
}


Why test works only in the first case?

In the second case renderIntoDocument return null.
I can't find any information about it.

So the question is - does JEST support rendering anonymous functions?

Answer

try changing

jest.dontMock('../js/components/COMPONENT_NAME/index');

to

jest.unmock('../js/components/COMPONENT_NAME/index');
Comments