如何在Jest测试中测试链接的诺言? [英] How can I test chained promises in a Jest test?

查看:43
本文介绍了如何在Jest测试中测试链接的诺言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,我对我的 login 操作进行了测试.我正在模拟Firebase函数,并想测试 signIn / signOut 函数是否被调用.

Below I have a test for my login actions. I'm mocking a Firebase function and want to test if the signIn/signOut functions are called.

测试通过.但是,我看不到第二个控制台日志. console.log('store ==>', store);这是哪一行.

The tests pass. However, I do not see my second console log. Which is this line console.log('store ==>', store);.

it('signIn should call firebase', () => {
  const user = {
    email: 'first.last@yum.com',
    password: 'abd123'
  };

  console.log('111');
  return store.dispatch(signIn(user.email, user.password)).then(() => {
    console.log('222'); // Does not reach
    expect(mockFirebaseService).toHaveBeenCalled();
  });
  console.log('333');
});

●登录操作›登录应致电Firebase

● login actions › signIn should call Firebase

TypeError:auth.signInWithEmailAndPassword不是函数

TypeError: auth.signInWithEmailAndPassword is not a function

// Sign in action
export const signIn = (email, password, redirectUrl = ROUTEPATH_DEFAULT_PAGE) => (dispatch) => {
  dispatch({ type: USER_LOGIN_PENDING });

  return firebase
    .then(auth => auth.signInWithEmailAndPassword(email, password))
    .catch((e) => {
      console.error('actions/Login/signIn', e);
      // Register a new user
      if (e.code === LOGIN_USER_NOT_FOUND) {
        dispatch(push(ROUTEPATH_FORBIDDEN));
        dispatch(toggleNotification(true, e.message, 'error'));
      } else {
        dispatch(displayError(true, e.message));
        setTimeout(() => {
          dispatch(displayError(false, ''));
        }, 5000);
        throw e;
      }
    })
    .then(res => res.getIdToken())
    .then((idToken) => {
      if (!idToken) {
        dispatch(displayError(true, 'Sorry, there was an issue with getting your token.'));
      }

      dispatch(onCheckAuth(email));
      dispatch(push(redirectUrl));
    });
};

全面测试

    import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';

// Login Actions
import {
  // onCheckAuth,
  signIn,
  signOut
} from 'actions';

import {
  // USER_ON_LOGGED_IN,
  USER_ON_LOGGED_OUT
} from 'actionTypes';

// String Constants
// import { LOGIN_USER_NOT_FOUND } from 'copy';

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

// Mock all the exports in the module.
function mockFirebaseService() {
  return new Promise(resolve => resolve(true));
}

// Since "services/firebase" is a dependency on this file that we are testing,
// we need to mock the child dependency.
jest.mock('services/firebase', () => new Promise(resolve => resolve(true)));

describe('login actions', () => {
  let store;

  beforeEach(() => {
    store = mockStore({});
  });

  it('signIn should call firebase', () => {
    const user = {
      email: 'first.last@yum.com',
      password: 'abd123'
    };

    console.log('111');
    return store.dispatch(signIn(user.email, user.password)).then(() => {
      console.log('222'); // does not reach
      expect(mockFirebaseService).toHaveBeenCalled();
    });
    console.log('333');
  });

  it('signOut should call firebase', () => {
    console.log('signOut should call firebasew');
    store.dispatch(signOut()).then(() => {
      expect(mockFirebaseService).toHaveBeenCalled();
      console.log('store ==>', store);
      expect(store.getActions()).toEqual({
        type: USER_ON_LOGGED_OUT
      });
    });
    console.log('END');
  });
});

推荐答案

您在这里遇到了两个问题,

You have two issues here,

测试通过了,但是我看不到第二个控制台日志.这是哪一个 行console.log('store ==>',store);.

The tests pass however I do not see my 2nd console log. Which is this line console.log('store ==>', store);.

那是因为测试不是在等待承诺的实现,所以您应该将其返回:

That is because the test is not waiting for the promise to fulfill, so you should return it:

it('signOut should call firebase', () => {
    console.log('signOut should call firebasew');
    return store.dispatch(signOut()).then(() => { // NOTE we return the promise
      expect(mockFirebaseService).toHaveBeenCalled();
      console.log('store ==>', store);
      expect(store.getActions()).toEqual({
        type: USER_ON_LOGGED_OUT
      });
      console.log('END');
    });

  });

您可以在 Redux官方文档中找到示例.

第二,您的登录测试失败,因为您嘲笑了错误的firebase:

Secondly, your signIn test is failing because you have mocked the wrong firebase:

jest.mock('services/firebase', () => new Promise(resolve => resolve(true)));

那应该看起来更像是:

jest.mock('services/firebase', () => new Promise(resolve => resolve({
    signInWithEmailAndPassword: () => { return { getIdToken: () => '123'; } }
})));

这篇关于如何在Jest测试中测试链接的诺言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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