【发布时间】:2020-08-29 23:17:15
【问题描述】:
我希望得到一些帮助。我研究这个问题太久了,我认为我没有提出任何问题。
提前感谢您的时间和关注,
我已经在互联网上搜索并尝试了很多东西,但我仍然没有运气 - 这就是问题所在。
总而言之 - 我正在跟随 https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning
将 react-testing-library 与 jest 结合使用 - 并遇到两个问题
Warning: An update to Login inside a test was not wrapped in act(...).- Jest 没有意识到正在调用模拟
我真的认为我已经正确连接了所有东西 - 不确定我错过了什么。 在 mock 中我可以放置一个 console.log 并且可以确认 mock 确实被调用了。
/*
Test
*/
it('should call props.fetchSignIn with the username and password', async () => {
const promise = Promise.resolve({
email: 'test@circulate.social',
firstName: 'Mike',
lastName: 'A',
});
const fetchSignIn = jest.fn(() => promise);
const { queryByTestId, queryByPlaceholderText } = renderLogin({
fetchSignIn,
});
const emailInput = queryByPlaceholderText('joedoe@gmail.com');
const passwordInput = queryByPlaceholderText('password');
const submitButton = queryByTestId('submitButton');
fireEvent.change(emailInput, {
target: { value: 'mike@circulate.social' },
});
fireEvent.change(passwordInput, { target: { value: 'Password1!' } });
fireEvent.click(submitButton);
// I expected this to fail from the parameters being wrong
expect(fetchSignIn).toHaveBeenCalledWith('asf');
await act(() => promise);
});
/*
Functional Component using the `useState(...)` hooks
onFormFinished is the form `onSubmit` handler
*/
const handleSignIn = async (
email: string,
password: string
): Promise<UserContextType['user']> => {
setIsLoginInFlight(true);
try {
const result = await props.fetchSignIn(email, password);
setIsLoginInFlight(false);
return result;
} catch (error) {
// Other logic removed
};
const onFormFinish = async (values: FormValues): Promise<void> => {
const { email, password } = values;
const signInResult = await handleSignIn(email, password);
// Other logic removed
};
有两件事正在发生
1 - 那个警告
2 - 测试失败,因为从未调用过 fetchSignIn
预期结果
调用fetchSignIn导致测试失败,但参数错误
欢迎任何意见或要求澄清。
谢谢
【问题讨论】:
-
这似乎是 antd 和 react-testing-library 的问题3 github.com/ant-design/ant-design/issues/21272 将其更改为
submit对我不起作用
标签: javascript reactjs unit-testing jestjs react-testing-library