我正在使用 jest 来使用快照测试。
我在 react-test-renderer 中遇到了一个错误,Invariant Violation: getNodeFromInstance: Invalid argument。
复制错误的最少代码:
import React from 'react';
import DateTime from 'react-datetime';
import CalendarContainer from 'react-datetime/src/CalendarContainer';
export default class CalendarTimer extends DateTime {
render() {
return ( <div className = "rdtPicker" >
<CalendarContainer view = {
this.state.currentView
}/>
</div>
);
}
}
这是测试规范文件
import React from 'react';
import renderer from 'react-test-renderer';
import CalendarTimer from 'components/Input/CalendarTimer';
describe('CalendarTimer', () => {
it('rendered Calendar', () => {
const calendarTimer = renderer.create( <
CalendarTimer / >
);
expect(calendarTimer).toMatchSnapshot();
});
});
错误:
● CalendarTimer › rendered Calendar
Invariant Violation: getNodeFromInstance: Invalid argument.
at invariant (node_modules/fbjs/lib/invariant.js:44:15)
at Object.getNodeFromInstance (node_modules/react-dom/lib/ReactDOMComponentTree.js:162:77)
at Object.findDOMNode (node_modules/react-dom/lib/findDOMNode.js:49:41)
at componentDidMount (node_modules/react-onclickoutside/index.js:153:40)
at chainedFunction [as componentDidMount] (node_modules/create-react-class/factory.js:617:11)
at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:265:25
at measureLifeCyclePerf (node_modules/react-test-renderer/lib/ReactCompositeComponent.js:75:12)
at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:264:11
at CallbackQueue.notifyAll (node_modules/react-test-renderer/lib/CallbackQueue.js:76:22)
at ReactTestReconcileTransaction.close (node_modules/react-test-renderer/lib/ReactTestReconcileTransaction.js:36:26)
at ReactTestReconcileTransaction.closeAll (node_modules/react-test-renderer/lib/Transaction.js:206:25)
at ReactTestReconcileTransaction.perform (node_modules/react-test-renderer/lib/Transaction.js:153:16)
at batchedMountComponentIntoNode (node_modules/react-test-renderer/lib/ReactTestMount.js:69:27)
at ReactDefaultBatchingStrategyTransaction.perform (node_modules/react-test-renderer/lib/Transaction.js:140:20)
at Object.batchedUpdates (node_modules/react-test-renderer/lib/ReactDefaultBatchingStrategy.js:62:26)
at Object.batchedUpdates (node_modules/react-test-renderer/lib/ReactUpdates.js:97:27)
at Object.render (node_modules/react-test-renderer/lib/ReactTestMount.js:125:18)
at Object.<anonymous> (tests/components/Input/CalendarTimer_spec.js:8:53)
at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
at process._tickCallback (internal/process/next_tick.js:103:7)
有人可以指出我做错了什么,并指导我解决同样的问题。