我正在使用带有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();
});
错误消息
react: ^16.3.2
jest: ^22.4.2
谢谢