How to properly make mock throw an error in Jest?
Change .mockReturnValue
with .mockImplementation
:
yourMockInstance.mockImplementation(() => {
throw new Error();
});
If it's a promise you can also to .rejects www.jestjs.io/docs/en/asynchronous#resolves--rejects
For Angular + Jest:
import { throwError } from 'rxjs';
yourMockInstance.mockImplementation(() => {
return throwError(new Error('my error message'));
});