The only way by now is accessing request headers in .reply and validate it here:
mockAxios.onPut(`xxxxx.com`).reply((config) => {
expect(config.headers."Content-Type").toEqual("What do you expect here");
return [200, expectedResult, {
Authorization: `Bearer ${token}`,
"Content-type": "application/x-www-form-urlencoded",
}];
});
Actually I believe it should also be possible in declarative way:
mockAxios.onPut(`xxxxx.com`, undefined, {
expectedHeader1: "value1",
expectedHeader2: "value2"}
).reply(200, expectedResult);
So it would just throw instead of returning mock response if request headers did not match.
But it does no work this way by now.
Reason: axios-mock-adapter uses deepEqual for such a filtering. So we would need specify there not just few required headers(we are focusing on) but all headers including those axios adds on its own(like Accept). So it is not really readable.
I've filed #219 in their repo on this. If it was not intentional for any reason, that may be fixed in future.
Answer from skyboyer on Stack Overflow
» npm install axios-mock-adapter
The only way by now is accessing request headers in .reply and validate it here:
mockAxios.onPut(`xxxxx.com`).reply((config) => {
expect(config.headers."Content-Type").toEqual("What do you expect here");
return [200, expectedResult, {
Authorization: `Bearer ${token}`,
"Content-type": "application/x-www-form-urlencoded",
}];
});
Actually I believe it should also be possible in declarative way:
mockAxios.onPut(`xxxxx.com`, undefined, {
expectedHeader1: "value1",
expectedHeader2: "value2"}
).reply(200, expectedResult);
So it would just throw instead of returning mock response if request headers did not match.
But it does no work this way by now.
Reason: axios-mock-adapter uses deepEqual for such a filtering. So we would need specify there not just few required headers(we are focusing on) but all headers including those axios adds on its own(like Accept). So it is not really readable.
I've filed #219 in their repo on this. If it was not intentional for any reason, that may be fixed in future.
The declarative way mentioned by @skyboyer works with objectContaining.
mockAxios.onPut(
`xxxxx.com`,
undefined,
expect.objectContaining({ expectedHeader1: "value1", expectedHeader2: "value2"})
).reply(200, expectedResult);
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