使用React测试库 /开玩笑测试错误边界:
我正在尝试为错误边界hoc编写测试。但是,当我模拟包装组件中的抛出错误时,我的测试会失败,因为我丢了相同的错误,就像它似乎没有意识到此错误是用于测试的。我不确定我在这里做错了什么。
这是errorboundary hoc:
interface StateProps {
error: unknown;
info: unknown;
}
interface ErrorBoundaryProps {
createNotification(...args: any): void;
}
const connector = connect(null, {
createNotification: createNotificationAction,
});
export const withErrorBoundary = <P extends object>(TargetComponent: React.ComponentType<P>) => {
class ErrorBoundary extends React.Component<ErrorBoundaryProps, StateProps> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = {
error: null,
info: null,
};
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
this.props.createNotification({
alertType: 'error',
message: `${TargetComponent.name} is not being rendered. Error: ${error.message}`,
});
this.setState({ error, info: errorInfo });
}
public render() {
const { ...props } = this.props;
if (this.state.error instanceof Error) {
return null;
}
return <TargetComponent {...(props as P)} />;
}
}
return connector(ErrorBoundary) as any;
};
这是测试:
describe('ErrorBoundary HOC', () => {
let store;
let createNotification;
let props;
beforeEach(() => {
store = configureStore(defaultState);
createNotification = jest.fn();
props = {
createNotification,
};
});
test('Renders nothing if error', async () => {
const targetComponent = () => {
throw new Error('Errored!');
};
const WrappedComponent = withErrorBoundary(targetComponent);
const RenderedComponent = render(
<BrowserRouter>
<Provider store={store}>
<WrappedComponent {...props} />
</Provider>
</BrowserRouter>
);
await waitFor(() => expect(() => WrappedComponent.toThrow()));
expect(RenderedComponent.container.hasChildNodes()).toBeFalsy();
await waitFor(() => expect(createNotification).toHaveBeenCalled());
});
});
到目前为止,我已经发现错误是在测试中呈现之前投掷的。但不确定如何解决这个问题。
感谢您提前的帮助。
I am trying to write test for my error boundary hoc. But when I mock throwing error in my wrapped component my test gets fail because of the same error I throw, like it seems like it doesnt recognize that this error was intended for testing. I am not sure what I am doing wrong here.
This is ErrorBoundary HOC:
interface StateProps {
error: unknown;
info: unknown;
}
interface ErrorBoundaryProps {
createNotification(...args: any): void;
}
const connector = connect(null, {
createNotification: createNotificationAction,
});
export const withErrorBoundary = <P extends object>(TargetComponent: React.ComponentType<P>) => {
class ErrorBoundary extends React.Component<ErrorBoundaryProps, StateProps> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = {
error: null,
info: null,
};
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
this.props.createNotification({
alertType: 'error',
message: `${TargetComponent.name} is not being rendered. Error: ${error.message}`,
});
this.setState({ error, info: errorInfo });
}
public render() {
const { ...props } = this.props;
if (this.state.error instanceof Error) {
return null;
}
return <TargetComponent {...(props as P)} />;
}
}
return connector(ErrorBoundary) as any;
};
and here is the test:
describe('ErrorBoundary HOC', () => {
let store;
let createNotification;
let props;
beforeEach(() => {
store = configureStore(defaultState);
createNotification = jest.fn();
props = {
createNotification,
};
});
test('Renders nothing if error', async () => {
const targetComponent = () => {
throw new Error('Errored!');
};
const WrappedComponent = withErrorBoundary(targetComponent);
const RenderedComponent = render(
<BrowserRouter>
<Provider store={store}>
<WrappedComponent {...props} />
</Provider>
</BrowserRouter>
);
await waitFor(() => expect(() => WrappedComponent.toThrow()));
expect(RenderedComponent.container.hasChildNodes()).toBeFalsy();
await waitFor(() => expect(createNotification).toHaveBeenCalled());
});
});
What I have been found so far is the error is throwing before render in the test. but not sure how to solve this.
Thanks for your help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在函数组件中,除了
useefffect()
中的内容外,在返回之前执行返回之前的所有内容。在这种情况下,
targetComponent
没有返回或JSX渲染,但仍然适用。您可以在这样的渲染之后丢弃错误,
我不确定是否足以为您提供所需的测试,但这是一个细致点。
In a functional component, everything before the return is executed before rendering, except for stuff in
useEfffect()
.In this case
targetComponent
has no return or JSX to render, but it still applies.You can throw the error after render like this
I'm not sure if it's enough to give you the desired test, but it's a strating point.