1

I made the following test using MockAdapter from axios-mock-adapter. However I'm trying to assert that the get function has effectively been called so I created a spy. For some reason it doesn't seem to work and I get:

expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0

Here's my test:

it('gets publications', async() => {

    let spy = jest.spyOn(axios, "get");
    var mock = new MockAdapter(axios);
    mock.onGet(PUBLICATIONS_PATH + '/publications').reply(200, 
        {
            answer: {
                publications: [ "pub1", "pub2", "pub3" ]
            }
        });

    let queryParameters = {
        operation: 'FSale'
    }


    const publications = await PublicationService.getPublications(queryParameters);

    expect(publications.data.answer.publications).toEqual([ "pub1", "pub2", "pub3" ]); // works fine
    expect(spy).toHaveBeenCalled(); //This fails
})

I was actually trying to use the approach answered here.

Update: Here's the code for getPublications

async function _getPublications(queryParameters){
  return await axios({
      method: 'get',
      url: `${PUBLICATIONS_PATH}/publications`,
      cancelToken: CancelTokenService.getSource().token,
      params: queryParameters,
      headers: {
        authorization: LocalStorageService.getAuthorization(),
        'Accept': ResourcesVersions.PUBLICATION
      }
  }).then(function (response){ return response }).catch(function (error){ return (axios.isCancel(error) ? error : error.response) })

}

Flama
  • 772
  • 3
  • 13
  • 39

3 Answers3

1

In the test code you provided you are spying on axios get method, but in the getPublications method you are not calling that method. Instead, you are calling the axios method directly.

As spying the axios default method is not easy, I would suggest to change the code in getPublications to use the get method:

async function _getPublications(queryParameters){
  return await axios.get(`${PUBLICATIONS_PATH}/publications`, {
      cancelToken: CancelTokenService.getSource().token,
      params: queryParameters,
      headers: {
        authorization: LocalStorageService.getAuthorization(),
        'Accept': ResourcesVersions.PUBLICATION
      }
  }).then(function (response){ return response }).catch(function (error){ return (axios.isCancel(error) ? error : error.response) })
}
mgarcia
  • 5,669
  • 3
  • 16
  • 35
1

You can use https://github.com/ctimmerm/axios-mock-adapter#history feature to check what actual calls have been made and assert on URLs, headers, methods and what-else.

0

I'm not used to use jest.spy on my test but I think you could try something like:

import axios from 'axios';

jest.mock('axios');
...

it('gets publications', async() => {

    const get = axios.get.mockResolvedValueOnce(yourMockedData)

    let queryParameters = {
        operation: 'FSale'
    }


    const publications = await PublicationService.getPublications(queryParameters);

    expect(publications.data.answer.publications).toEqual([ "pub1", "pub2", "pub3" ]); // works fine
    expect(get).toHaveBeenCalled(); //This fails
})
Ian Vasco
  • 1,280
  • 13
  • 17
  • I appreciate your answer but I'm looking to keep using axios-mock-adapter. – Flama Jan 29 '20 at 01:47
  • Alright, just wondering if you have tried using a promise instead of async await – Ian Vasco Jan 29 '20 at 13:43
  • @Stackoverflowed on the other side, I'd rather make a separate test for PublicationService mocking axios and then using a mocked response for PublicationService in this test. The idea is not to have dependencies. – Ian Vasco Jan 29 '20 at 13:49