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

隐身守侯 提交于 2019-12-10 13:27:30

问题


I have a passing test now thanks to the answer here: How to test is chained promises in a jest test?

However I'm still getting an error in the catch part of my test.

I seem to not be able to correctly mock or spy this part in the actions file: .then(res => res.getIdToken())

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

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

回答1:


It looks like the reason why you're getting this error has to do with the data you're mocking through Jest.

Try mocking your getIdToken as a function, rather than a string:

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


来源:https://stackoverflow.com/questions/48508225/how-to-fix-typeerror-is-not-a-function-testing-promises-with-jest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!