简体   繁体   English

如何修复 TypeError 不是函数(用 Jest 测试 promise)

[英]How to fix TypeError is not a function (testing promises with Jest)

I have a passing test now thanks to the answer here: How to test is chained promises in a jest test?由于这里的答案,我现在通过了测试: How to test is chained promises in a jest test?

However I'm still getting an error in the catch part of my test.但是,我在测试的 catch 部分仍然遇到错误。

I seem to not be able to correctly mock or spy this part in the actions file: .then(res => res.getIdToken())我似乎无法在操作文件中正确模拟或监视此部分: .then(res => res.getIdToken())

TEST signIn ERROR => TypeError: res.getIdToken is not a function测试登录错误 => 类型错误:res.getIdToken 不是函数

在此处输入图片说明

The Test测试

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);
      });
  });

The SignIn actions/Login登录操作/登录

// 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));
    });
};

It looks like the reason why you're getting this error has to do with the data you're mocking through Jest.看起来您收到此错误的原因与您通过 Jest 模拟的数据有关。

Try using jest.fn() to mock your getIdToken as a function, rather than a string:尝试使用jest.fn()将您的getIdToken模拟为一个函数,而不是一个字符串:

const mockGetIdToken = jest.fn(() => '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);
      });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决错误 TypeError: x is not a function for testing the following function in Jest? - How to resolve the error TypeError: x is not a function for testing of the following function in Jest? 在Jest中测试嵌套的Promise - Testing nested promises in Jest 玩笑测试:TypeError: (0 , _rtb.disableAllAds) is not a function - Jest testing: TypeError: (0 , _rtb.disableAllAds) is not a function 开玩笑-测试mobx存储@action TypeError:不是函数 - jest - testing mobx store @action TypeError: is not a function 用于承诺的非 function 的笑话错误 - jest errors with not a not a function for promises 开玩笑的单元测试失败,并显示TypeError:validemail.checkValidity不是函数,如何解决 - Jest unit test fails with TypeError: validemail.checkValidity is not a function, how to fix 'TypeError: store.dispatch is not a function' redux action testing with jest - 'TypeError: store.dispatch is not a function' redux action testing with jest Angular 6 和 Jest 的测试出错 - TypeError: testing.TestBed.inject is not a function - Error in tests with Angular 6 and Jest - TypeError: testing.TestBed.inject is not a function React/Jest:测试 React Context 方法(结果:TypeError:setScore 不是函数) - React/Jest: Testing React Context methods (Result: TypeError: setScore is not a function) 测试具有多个承诺的Jest方法 - Testing a Jest method that has multiple promises
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM