我在我的函数组件中使用 useAsync() 钩子。该组件在上一步中单击下一步按钮后在 material-ui stepper 的最后一步呈现,这是一个单独的组件。
const memberStatus = useAsync(async () => {
await addMembersToTeam(props.projectName, props.teamName, props.members);
}, []);
以下是我的测试片段:
const promise = new Promise<any>((_resolve, reject) => {
reject(new Error('failed'))
})
mocked(mockAddMembersToTeam).mockRejectedValue(promise)
const nextButton = await rendered.findByRole('button', { name: /next/ });
expect(nextButton).toBeEnabled();
userEvent.click(nextButton);
await waitFor(() => { expect(mockAddMembersToTeam).toHaveBeenCalledOnce() })
const errorAlert = await waitFor(() =>
rendered.getByTestId('membersStatusErrorMsg'),
);
const expectedAlert = 'Issue Adding Members.';
expect(errorAlert.textContent).toBe(expectedAlert);
测试失败,如下:
ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down.
failed
281 | const promise = new Promise<any>((_resolve, reject) => {
> 282 | reject(new Error('failed'))
| ^
283 | })
我感谢对此问题的任何见解。谢谢。