JEST+酶扩增Auth的检测 [英] Testing amplify Auth with Jest + Enzyme

查看:12
本文介绍了JEST+酶扩增Auth的检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对测试非常陌生,我终于觉得我已经掌握了它的诀窍。然而,嘲弄仍然有点令人困惑。我目前正在测试一个注册功能,该功能将一直执行到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');
})

这篇关于JEST+酶扩增Auth的检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆