Jest: supports mockFn.mockThrowError, mockThrowErrorOnce

Created on 24 Nov 2016  路  1Comment  路  Source: facebook/jest

Do you want to request a feature or report a bug?

feature

What is the current behavior?

we can throw errors using mockImplementation.

mockFn.mockImplementation(() => { throw new Error('adbd') });
mockFn.mockImplementationOnce(() => { throw new TypeError('adbd') });

but, ideally i'd like to see something like:

mockFn.mockThrowError('adbd');
mockFn.mockThrowErrorOnce(new TypeError('adbd'));

What is the expected behavior?

testcase:

it('supports throw errors', () => {
  const fn = moduleMocker.getMockFunction();
  fn.mockThrowError('abcd');

  expect(fn).toThrowError('abcd');

  const fn2 = moduleMocker.getMockFunction();
  fn.mockThrowError(new TypeError('abcd'));

  expect(fn2).toThrowError(TypeError);
});

>All comments

Since you can write a one-liner function/wrapper around it, I don't see the reason for extending mocks API just to do this.
I'm closing the issue for now to manage the queue, but we can keep the discussion going and reopen if necessary.

Was this page helpful?
0 / 5 - 0 ratings