91

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 one function (namely, Meteor's callMethod) that is used in mutations.

  it('should throw error if email not found', async () => {
    callMethod
      .mockReturnValue(new Error('User not found [403]'))
      .mockName('callMethod');

    const query = FORGOT_PASSWORD_MUTATION;
    const params = { email: 'user@example.com' };

    const result = await simulateQuery({ query, params });

    console.log(result);

    // test logic
    expect(callMethod).toBeCalledWith({}, 'forgotPassword', {
      email: 'user@example.com',
    });

    // test resolvers
  });

When I console.log(result) I get

{ data: { forgotPassword: true } }

This behaviour is not what I want because in .mockReturnValue I throw an Error and therefore expect result to have an error object

Before this test, however, another is ran

 it('should throw an error if wrong credentials were provided', async () => {
    callMethod
      .mockReturnValue(new Error('cannot login'))
      .mockName('callMethod');

And it works fine, the error is thrown

I guess the problem is that mock doesn't get reset after the test finishes. In my jest.conf.js I have clearMocks: true

Each test suit is in a separate file, and I mock functions before tests like this:

import simulateQuery from '../../../helpers/simulate-query';

import callMethod from '../../../../imports/api/users/functions/auth/helpers/call-accounts-method';

import LOGIN_WITH_PASSWORD_MUTATION from './mutations/login-with-password';

jest.mock(
  '../../../../imports/api/users/functions/auth/helpers/call-accounts-method'
);

describe('loginWithPassword mutation', function() {
...

UPDATE

When I substituted .mockReturnValue with .mockImplementation everything worked out as expected:

callMethod.mockImplementation(() => {
  throw new Error('User not found');
});

But that doesn't explain why in another test .mockReturnValue works fine...

dwjohnston
  • 7,389
  • 20
  • 81
  • 147
Le garcon
  • 3,929
  • 5
  • 23
  • 38
  • It looks like your mock is *returning* an error object, not _throwing_ it. Without seeing your code that you are testing, I can only share the experience I had. I forgot to mock a function called in my mutation, which caused an error to be thrown unintentionally. Perhaps there is something similar happening for you? – Rhuarc13 May 14 '18 at 13:54

2 Answers2

170

Change .mockReturnValue with .mockImplementation:

yourMockInstance.mockImplementation(() => {
  throw new Error();
});
eduardomoroni
  • 2,192
  • 1
  • 16
  • 12
7

For Angular + Jest:

import { throwError } from 'rxjs';

yourMockInstance.mockImplementation(() => {
  return throwError(new Error('my error message'));
});
Sean W
  • 447
  • 5
  • 13
AngularBoy
  • 1,121
  • 1
  • 16
  • 30
  • 1
    Technically this isn't a `throw` in the pure JS sense. You are configuring the mock to return a RXJS observable object which immediately emits an error notification. Still, maybe handy for folks to see here. The accepted answer certainly *will* make a mock throw an error. In all cases. – Zach Lysobey Sep 16 '20 at 21:57
  • Only returning `throw Error` should be enough: `yourMockInstance.mockImplementation(() => throwError('my error message'));` – manzapanza Apr 30 '21 at 21:42