gpt4 book ai didi

reactjs - 使用 Jest + Enzyme 测试放大身份验证

转载 作者:行者123 更新时间:2023-12-03 18:44:15 26 4
gpt4 key购买 nike

我对测试很陌生,我终于觉得我已经掌握了它。然而,mocks 仍然有点令人困惑。我目前正在测试注册功能,这些功能会一直执行到 Auth.signUp .我不确定我是否需要在我的测试中模拟某些东西,或者我是否需要它来运行不同的测试。

async function signUp(
{ first, last, email, password }: SignupUserType,
dispatch: Dispatcher,
formContent: FormContentType,
setFormContent: SetFormContent,
) {
console.log('signing in init...');
dispatch({ type: 'INIT' });

try {
const user = await Auth.signUp({
username: email,
password,
attributes: {
given_name: first,
family_name: last,
picture: userImage,
},
});
console.log('sign up success!');
dispatch({ type: 'STOP_LOADING' });
console.log(formContent);
setFormContent(formContent);
} catch (err) {
console.log('error signing up...', err);
dispatch({ type: 'ERROR', error: err.message, doing: 'SIGNUP' });
}
}

测试
import Amplify, { Auth } from 'aws-amplify';
import awsconfig from '../../../aws-exports';

Amplify.configure(awsconfig);

jest.mock('aws-amplify);
it('SIGNUP: Completed form fields enable button', async () => {
...
wrapper
.find('#submitButton')
.at(0)
.simulate('click');

// thought I could do something like from https://stackoverflow.com/questions/51649891/how-to-mock-aws-library-in-jest
Auth.signUp = jest.fn().mockImplementation(
() => {
// return whatever you want to test
});

// or I tried something like from https://markpollmann.com/testing-react-applications
expect(Amplify.Auth.signUp).toHaveBeenCalled();
// kept getting errors about not receiving the call
})

最佳答案

我让它工作了!

import Amplify, { Auth } from 'aws-amplify';
import awsconfig from '../../../aws-exports';

Amplify.configure(awsconfig);


Auth.signUp = jest.fn().mockImplementation(
() => {
return true;
});


it('SIGNUP: Completed form fields enable button', async () => {
...
wrapper
.find('#submitButton')
.at(0)
.simulate('click');
})

关于reactjs - 使用 Jest + Enzyme 测试放大身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60700025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com