I am trying to write a test that asserts that a specific type of error is thrown with Async / Await and axios. However, when I run my test, I get the following. Why is jest not properly rejecting my promise? Thanks!
Error: expect(received).rejects.toThrow()
Expected received Promise to reject, instead it resolved to value
{"data": "response", "status": 404}
api.js:
import axios from 'axios';
import SpecialError from './specialError.js';
const get = async () => {
try {
const response = await axios.get('sampleUrl', { withCredentials: true });
return response;
} catch (error) {
throw new SpecialError(error);
}
};
export default get;
specialError.js:
export default class SpecialError extends Error {
constructor() {
super();
this.isSpecialError = true;
}
}
api.test.js:
import axios from 'axios';
import get from './api';
import SpecialError from './specialError.js';
test('testing the api get method', async () => {
axios.get.mockImplementation(() => Promise.resolve({
data: 'response',
status: 404,
}));
const expectedError = new SpecialError('foo');
await expect(get()).rejects.toEqual(expectedError);
});