Closed
Description
Jest snapshot testing uses ReactTestRenderer but if my component contains a ref or uses ReactDOM.findDOMNode
it throws TypeError: component.getPublicInstance is not a function
.
Component
import React from 'react';
export default class Link extends React.Component {
render() {
return (
<a
ref={a => this._a = a}
href={this.props.page || '#'}>
{this.props.children}
</a>
);
}
}
Test
'use strict'
import React from 'react';
import Link from '../Link';
import renderer from 'react-test-renderer';
describe('Link', () => {
it('renders correctly', () => {
const tree = renderer.create(
<Link page="foo" />
).toJSON();
expect(tree).toMatchSnapshot();
});
});
stack trace
FAIL __tests__/Link-test.js (2.148s)
● Link › it renders correctly
- TypeError: component.getPublicInstance is not a function
at attachRef (node_modules/react/lib/ReactRef.js:20:19)
at Object.ReactRef.attachRefs (node_modules/react/lib/ReactRef.js:42:5)
at attachRefs (node_modules/react/lib/ReactReconciler.js:26:12)
at CallbackQueue._assign.notifyAll (node_modules/react/lib/CallbackQueue.js:67:22)
at ReactTestReconcileTransaction.ON_DOM_READY_QUEUEING.close (node_modules/react/lib/ReactTestReconcileTransaction.js:37:26)
at ReactTestReconcileTransaction.Mixin.closeAll (node_modules/react/lib/Transaction.js:204:25)
at ReactTestReconcileTransaction.Mixin.perform (node_modules/react/lib/Transaction.js:151:16)
at batchedMountComponentIntoNode (node_modules/react/lib/ReactTestMount.js:61:27)
at ReactDefaultBatchingStrategyTransaction.Mixin.perform (node_modules/react/lib/Transaction.js:138:20)
at Object.ReactDefaultBatchingStrategy.batchedUpdates (node_modules/react/lib/ReactDefaultBatchingStrategy.js:63:19)