javascript – 如何修复TypeError不是一个函数(使用Jest测试promise)

由于答案在这里,我现在通过测试:How to test is chained promises in a jest test?

但是我在测试的catch部分仍然出错.

我好像无法在动作文件中正确地模拟或侦察这部分:.then(res => res.getIdToken())

TEST signIn ERROR => TypeError: res.getIdToken is not a function

javascript  – 如何修复TypeError不是一个函数(使用Jest测试promise)

考试

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

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

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

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

    return store.dispatch(signIn(user.email, user.password))
      .then(() => {
        console.log('TEST signIn SUCCESS');
        expect(mockSignIn).toHaveBeenCalled();
        expect(store.getActions()).toEqual({
          type: USER_ON_LOGGED_IN
        });
      })
      .catch((err) => {
        console.log('TEST signIn ERROR =>', err);
      });
  });

SignIn操作/登录

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

  return firebase
    .then((auth) => {
      console.log('auth =>', auth);
      return 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;
      }
    })

    // I can't seem to mock this correctly
    .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));
    });
};

解决方法:

看起来你得到这个错误的原因与你通过Jest嘲笑的数据有关.

尝试将getIdToken模拟为函数,而不是字符串:

const mockGetIdToken = () => 'abc123';

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

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

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

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

    return store.dispatch(signIn(user.email, user.password))
      .then(() => {
        console.log('TEST signIn SUCCESS');
        expect(mockSignIn).toHaveBeenCalled();
        expect(store.getActions()).toEqual({
          type: USER_ON_LOGGED_IN
        });
      })
      .catch((err) => {
        console.log('TEST signIn ERROR =>', err);
      });
  });
上一篇:javascript – 如何在redux操作(或操作中分配的其他回调)的上下文中使用jest测试`image.onload`


下一篇:These dependencies were not found:core-js/modules/es.array.concat in ./node_modules/cache-loader/dis