最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to see what the rendered React component looks like in the Jest unit test? - Stack Overflow

programmeradmin5浏览0评论

I'm trying to run a test for the React ponent. I need to check how it looks like after rendering. Tried to use ReactDOMServer.renderToString() but it fails. Here is the code:

import { NewRec } from '../src/ponents/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 ponent', () => {
    const ponent = shallow(<NewRec />);
    it('returns true if blah blah', ()=>{
        var htmlstring = ReactDOMServer.renderToString(<ponent />);
    });
});

I'm getting the following error:

Invariant Violation: There is no registered ponent for the tag ponent

I tried to call it like: var htmlstring = ReactDOMServer.renderToString(ponent); then I get this error:

Invariant Violation: renderToString(): You must pass a valid ReactElement.

Does anyone knows where the problem is?

I'm trying to run a test for the React ponent. I need to check how it looks like after rendering. Tried to use ReactDOMServer.renderToString() but it fails. Here is the code:

import { NewRec } from '../src/ponents/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 ponent', () => {
    const ponent = shallow(<NewRec />);
    it('returns true if blah blah', ()=>{
        var htmlstring = ReactDOMServer.renderToString(<ponent />);
    });
});

I'm getting the following error:

Invariant Violation: There is no registered ponent for the tag ponent

I tried to call it like: var htmlstring = ReactDOMServer.renderToString(ponent); then I get this error:

Invariant Violation: renderToString(): You must pass a valid ReactElement.

Does anyone knows where the problem is?

Share Improve this question edited Nov 9, 2016 at 22:09 Andreas Köberle 111k58 gold badges280 silver badges307 bronze badges asked Nov 9, 2016 at 10:01 BirishBirish 5,8327 gold badges36 silver badges56 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

There is the snapshot feature in Jest where it stores the rendered tree as a file. Note that you have to install enzyme-to-json as well to convert the enzyme rendered ponent to something the snapshot method can understand.

import { NewRec } from '../src/ponents/edit';
import { shallow } from 'enzyme';
import { shallowToJson } from 'enzyme-to-json';

describe('NewRec ponent', () = > {
  it('returns true if blah blah', () = > {
    const ponent = shallow(<NewRec />);
    expect(shallowToJson(ponent)).toMatchSnapshot();
  });
});

This will create a new file in __snapshot__ folder in your test folder, where you can inspect the rendered result. Every time you rerun the test, the ponent will be tested against the snapshot.

If you don't use enzyme, you can use Facebook's react-test-renderer too, it's even simpler:

import React from "react";
import renderer from "react-test-renderer";

test("Test 1", () => {
  const ponent = renderer.create(
    <TestItem />
  );

  let tree = ponent.toJSON();
  expect(tree).toMatchSnapshot();
});
发布评论

评论列表(0)

  1. 暂无评论