使用 Jest + Enzyme 测试放大 Auth
Testing amplify Auth with Jest + Enzyme
我对测试还很陌生,我终于觉得我掌握了它的窍门。然而,模拟仍然有点混乱。我目前正在测试注册功能,该功能会一直执行到 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
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');
})
感谢提示!
但是,在我的例子中,Auth.forgotPassword mock 的实现必须 return new Promise,因为在代码中我使用它
Auth.forgotPassword(email)
.then(...)
.catch(...)
所以我的模拟是:
Auth.forgotPassword = jest.fn().mockImplementation(() => {
return new Promise((resolve, reject) => {
resolve({
CodeDeliveryDetails: {
AttributeName: 'email',
DeliveryMedium: 'EMAIL',
Destination: 's***@y***.ru',
},
})
})
})
希望它能帮助遇到同样问题的人
另一种选择是使用:
jest.spyOn(Auth, "signUp").mockImplementation(() => {});
通过这个你可以省略jest.mock('aws-amplify');
并且只需要导入“Auth”而不是Amplify本身
我对测试还很陌生,我终于觉得我掌握了它的窍门。然而,模拟仍然有点混乱。我目前正在测试注册功能,该功能会一直执行到 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
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');
})
感谢提示!
但是,在我的例子中,Auth.forgotPassword mock 的实现必须 return new Promise,因为在代码中我使用它
Auth.forgotPassword(email)
.then(...)
.catch(...)
所以我的模拟是:
Auth.forgotPassword = jest.fn().mockImplementation(() => {
return new Promise((resolve, reject) => {
resolve({
CodeDeliveryDetails: {
AttributeName: 'email',
DeliveryMedium: 'EMAIL',
Destination: 's***@y***.ru',
},
})
})
})
希望它能帮助遇到同样问题的人
另一种选择是使用:
jest.spyOn(Auth, "signUp").mockImplementation(() => {});
通过这个你可以省略jest.mock('aws-amplify');
并且只需要导入“Auth”而不是Amplify本身