{"id":650,"hash":"a1714ef2bc0a2ed28de393f4b0bc0dd93fc21221ee311d2c9b6348a11f2ca18c","pattern":"How to properly make mock throw an error in Jest?","full_message":"I'm testing my GraphQL api using Jest.\n\nI'm using a separate test suit for each query/mutation\n\nI have 2 tests (each one in a separate test suit) where I mock one function (namely, Meteor's callMethod) that is used in mutations.\n\n  it('should throw error if email not found', async () => {\n    callMethod\n      .mockReturnValue(new Error('User not found [403]'))\n      .mockName('callMethod');\n\n    const query = FORGOT_PASSWORD_MUTATION;\n    const params = { email: 'user@example.com' };\n\n    const result = await simulateQuery({ query, params });\n\n    console.log(result);\n\n    // test logic\n    expect(callMethod).toBeCalledWith({}, 'forgotPassword', {\n      email: 'user@example.com',\n    });\n\n    // test resolvers\n  });\n\nWhen I console.log(result) I get       \n\n{ data: { forgotPassword: true } }\n\nThis behaviour is not what I want because in .mockReturnValue I throw an Error and therefore expect result to have an error object\n\nBefore this test, however, another is ran\n\n it('should throw an error if wrong credentials were provided', async () => {\n    callMethod\n      .mockReturnValue(new Error('cannot login'))\n      .mockName('callMethod');\n\nAnd it works fine, the error is thrown\n\nI guess the problem is that mock doesn't get reset after the test finishes.\nIn my jest.conf.js I have   clearMocks: true\n\nEach test suit is in a separate file, and I mock functions before tests like this:\n\nimport simulateQuery from '../../../helpers/simulate-query';\n\nimport callMethod from '../../../../imports/api/users/functions/auth/helpers/call-accounts-method';\n\nimport LOGIN_WITH_PASSWORD_MUTATION from './mutations/login-with-password';\n\njest.mock(\n  '../../../../imports/api/users/functions/auth/helpers/call-accounts-method'\n);\n\ndescribe('loginWithPassword mutation', function() {\n...\n\nUPDATE\n\nWhen I substituted .mockReturnValue with .mockImplementation everything worked out as expected:\n\ncallMethod.mockImplementation(() => {\n  throw new Error('User not found');\n});\n\nBut that doesn't explain why in another test .mockReturnValue works fine...","ecosystem":"npm","package_name":"meteor","package_version":null,"solution":"Change .mockReturnValue with .mockImplementation:\n\n    yourMockInstance.mockImplementation(() => {\n      throw new Error();\n    });\n\nin case you want to assert\n\n   test('the fetch fails with an error', () => {\n     return expect(fetchData()).rejects.toMatch('error');\n   });\n\nIf it's a promise you can also to .rejects www.jestjs.io/docs/en/asynchronous#resolves--rejects","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/49835264/how-to-properly-make-mock-throw-an-error-in-jest","votes":224,"created_at":"2026-04-19T04:51:27.258319+00:00","updated_at":"2026-04-19T04:51:27.258319+00:00"}