jeudi 4 mai 2017

Errors when trying to mock an async fetch function in Jest spec

I'm writing tests for my app but running into problems when I try to mock an async fetch function. No matter what I try, I get:

asyncFetch.mockReturnValue is not a function. Which leads me to believe the function is not being mocked. However, I do not know where the problem lies.

This is the test:

import emailSignin from 'api/emailSignin';

jest.mock('api/helpers/asyncFetch');

describe('API Auth', () => {
  describe('login', () => {
    it('has a happy path', async () => {
      const asyncFetch = require('api/helpers/asyncFetch').default;

      asyncFetch.mockReturnValue({
        json: () => 'it worked',
      });

      const response = await emailSignin('bob', 'password');
      expect(response).toEqual('it worked');
    });
  });
});

Any help would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire