使用ComponentDidCatch快照

时间:2018-07-18 07:49:03

标签: javascript reactjs jestjs

我正在使用带有Snapshot的componentDidCatch测试HOC,它正在工作,但是在终端上显示了令人讨厌的消息。你知道我怎么藏起来吗?

with-error.js

import React from 'react';
import { wrapDisplayName, setDisplayName } from 'recompose';

const ErrorPage = () => <h1>There is an error</h1>;

export default hasError => BaseComponent => {
    class WithErrorBoundary extends React.Component {
        state = { error: false };

        componentWillReceiveProps(nextProps) {
            if (hasError(nextProps) && !this.state.error) {
                this.setState({ error: true });
            }
        }

        componentDidCatch() {
            this.setState({ error: true });
        }

        render() {
            return this.state.error ? <ErrorPage /> : <BaseComponent {...this.props} />;
        }
    }

    return setDisplayName(wrapDisplayName(BaseComponent, 'ErrorCatcher'))(WithErrorBoundary);
};

with-error.test.js

it('should display the error page if it catchs an error', () => {
        const Component = withError()(() => {
            throw new Error('Error');
        });
        const subject = renderer
            .create(
                <Component />
            )
            .toJSON();
        expect(subject).toMatchSnapshot();
    });

错误消息

enter image description here

react: ^16.3.2

jest: ^22.4.2

谢谢

0 个答案:

没有答案