I am trying to test an axios get request with axios-mock-adapter
, such that an error is thrown given a status that does not equal 200. However, when I execute the test (see api.test.js
), I get the following message:
Error: expect(function).toThrowError(undefined)
Expected the function to throw an error. But it didn't throw anything.
How do I test to make sure that an error is thrown, using axios-mock-adapter
using my get
and handleResponse
methods? Thanks!
api.test.js:
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
const handleResponse = (response) => {
if (response.status !== 200) {
throw new Error('foo');
} else {
return response.data;
}
};
const get = async (url) => {
const response = await axios.get(url, {withCredentials: true});
return handleResponse(response);
};
test('testing that, when making a get request to the appropriate url, an error is thrown ' +
' when status !== 200', async () => {
new MockAdapter(axios)
.onGet('sampleUrl')
.reply(500, {data: 'payload'});
const expectedError = async () => {
await get('sampleUrl');
};
expect(expectedError).toThrowError();
});