我正在尝试为 React 组件运行测试。我需要检查渲染后的样子。尝试使用,ReactDOMServer.renderToString()
但失败。这是代码:
import { NewRec } from '../src/components/edit';
import { shallow } from 'enzyme';
import React from 'react/lib/ReactWithAddons';
import ReactDOMServer from 'react-dom/server';
jest.mock('react-dom');
jest.mock('react/lib/ReactDefaultInjection');
describe('NewRec component', () => {
const component = shallow(<NewRec />);
it('returns true if blah blah', ()=>{
var htmlstring = ReactDOMServer.renderToString(<component />);
});
});
我收到以下错误:
Invariant Violation: There is no registered component for the tag component
我试着这样称呼它:var htmlstring = ReactDOMServer.renderToString(component);
然后我收到这个错误:
Invariant Violation: renderToString(): You must pass a valid ReactElement.
有谁知道问题出在哪里?