Without using any other libraries:
import * as axios from "axios";
// Mock out all top level functions, such as get, put, delete and post:
jest.mock("axios");
// ...
test("good response", () => {
axios.get.mockImplementation(() => Promise.resolve({ data: {...} }));
// ...
});
test("bad response", () => {
axios.get.mockImplementation(() => Promise.reject({ ... }));
// ...
});
It is possible to specify the response code:
axios.get.mockImplementation(() => Promise.resolve({ status: 200, data: {...} }));
It is possible to change the mock based on the parameters:
axios.get.mockImplementation((url) => {
if (url === 'www.example.com') {
return Promise.resolve({ data: {...} });
} else {
//...
}
});
Jest v23 introduced some syntactic sugar for mocking Promises:
axios.get.mockImplementation(() => Promise.resolve({ data: {...} }));
It can be simplified to
axios.get.mockResolvedValue({ data: {...} });
There is also an equivalent for rejected promises: mockRejectedValue.
Further Reading:
- Jest mocking documentation
- A GitHub discussion that explains about the scope of the
jest.mock("axios")line. - A related question which addresses applying the techniques above to Axios request interceptors.
- Using jest functions like
mockImplementationin TypeScript: Typescript and Jest: Avoiding type errors on mocked functions
» npm install axios-mock-adapter
Videos
Without using any other libraries:
import * as axios from "axios";
// Mock out all top level functions, such as get, put, delete and post:
jest.mock("axios");
// ...
test("good response", () => {
axios.get.mockImplementation(() => Promise.resolve({ data: {...} }));
// ...
});
test("bad response", () => {
axios.get.mockImplementation(() => Promise.reject({ ... }));
// ...
});
It is possible to specify the response code:
axios.get.mockImplementation(() => Promise.resolve({ status: 200, data: {...} }));
It is possible to change the mock based on the parameters:
axios.get.mockImplementation((url) => {
if (url === 'www.example.com') {
return Promise.resolve({ data: {...} });
} else {
//...
}
});
Jest v23 introduced some syntactic sugar for mocking Promises:
axios.get.mockImplementation(() => Promise.resolve({ data: {...} }));
It can be simplified to
axios.get.mockResolvedValue({ data: {...} });
There is also an equivalent for rejected promises: mockRejectedValue.
Further Reading:
- Jest mocking documentation
- A GitHub discussion that explains about the scope of the
jest.mock("axios")line. - A related question which addresses applying the techniques above to Axios request interceptors.
- Using jest functions like
mockImplementationin TypeScript: Typescript and Jest: Avoiding type errors on mocked functions
I used axios-mock-adapter. In this case the service is described in ./chatbot. In the mock adapter you specify what to return when the API endpoint is consumed.
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import chatbot from './chatbot';
describe('Chatbot', () => {
it('returns data when sendMessage is called', done => {
var mock = new MockAdapter(axios);
const data = { response: true };
mock.onGet('https://us-central1-hutoma-backend.cloudfunctions.net/chat').reply(200, data);
chatbot.sendMessage(0, 'any').then(response => {
expect(response).toEqual(data);
done();
});
});
});
You can see it the whole example here:
Service: https://github.com/lnolazco/hutoma-test/blob/master/src/services/chatbot.js
Test: https://github.com/lnolazco/hutoma-test/blob/master/src/services/chatbot.test.js
Note: This answer is now outdated, see this answer by Laszlo Sarvold instead.
axios-mock-adapter does not appear to have this functionality built in to it, however if you are using jest, you can use jest.spyOn.
For your example above
let spy = jest.spyOn(axios, "get");
//run http request here
expect(spy).toHaveBeenCalled();
Note: depending on what you are using, you may have to wrap your expect statement in setTimeout(function, 0) for it to work properly
As per https://github.com/ctimmerm/axios-mock-adapter there is an out of the box functionality for verifying request calls:
expect(mock.history.post.length).toBe(1); // times called
expect(mock.history.post[0].data).toBe(JSON.stringify({ foo: "bar" })); // posted object
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
})
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) })
}