How to properly make mock throw an error in Jest?

前端 未结 2 717
悲哀的现实
悲哀的现实 2021-02-03 16:26

I\'m testing my GraphQL api using Jest.

I\'m using a separate test suit for each query/mutation

I have 2 tests (each one in a separate test suit) where I mock on

相关标签:
2条回答
  • 2021-02-03 17:06

    For Angular + Jest:

    import { throwError } from 'rxjs';
    
    yourMockInstance.mockImplementation(() => {
      return throwError(new Error('my error message'));
    });
    
    0 讨论(0)
  • 2021-02-03 17:11

    Change .mockReturnValue with .mockImplementation:

    yourMockInstance.mockImplementation(() => {
      throw new Error();
    });
    
    0 讨论(0)
提交回复
热议问题