Do you want to request a bug?
Bug
To reproduce the bug, please kindly clone https://github.com/zulucoda/-jest-mock-promise-reject and npm install, then run tests. npm tests
What is the current behaviour?
when mocking Promise.reject the number of mock.calls is incorrect.
service.onLogging = jest.fn(() => Promise.reject('some error'));
const dispatch = jest.fn();
await onLogging({ username: 'some username', password: 'some password' })(dispatch);
console.log('dispatch:', dispatch.mock.calls);
expect(dispatch.mock.calls[ 0 ][ 0 ]).toEqual(LoginLoading());
expect(dispatch.mock.calls[ 1 ][ 0 ]).toEqual(LoginError());
If the current behaviour is a bug, please provide the steps to reproduce, a minimal repository on GitHub that we can npm install and npm test.
To reproduce the bug, please kindly clone https://github.com/zulucoda/-jest-mock-promise-reject and npm install, then run tests. npm tests
What is the expected behaviour?
The expected behaviour is for mock.calls to include all the calls executed in the catch function of the Promise, just like all calls are executed if the Promise resolves.
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
no Jest config, using create-react-app
npm version: { 'jest-mock-promise-reject': '0.1.0',
npm: '4.5.0',
ares: '1.10.1-DEV',
http_parser: '2.7.0',
icu: '58.2',
modules: '48',
node: '6.10.1',
openssl: '1.0.2k',
uv: '1.9.1',
v8: '5.1.281.95',
zlib: '1.2.8' }
operating system: Ubuntu Linux
I think you should return a promise here
https://github.com/zulucoda/-jest-mock-promise-reject/blob/master/src/login.js#L42
->
return service.onLogging(...)
This is not a bug, it's just your code is tested the wrong way.
You dispatch an action which is synchronous and expect it to be async โ so you either need to return a promise from you action (like @lsentkiewicz pointed) or test it differently (probably what you want).
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions. You'll find more on Jest docs help page. Thank you :)
@lsentkiewicz @thymikee thanks guys for getting back to me.
As you will see from the test file there are two tests, one test resolves the promise the other test rejects the promise.
Test 1 - resolve promise - This works fine
https://github.com/zulucoda/-jest-mock-promise-reject/blob/master/src/login.test.js#L16
service.onLogging = jest.fn(() => Promise.resolve({}));
The dispatch function which is mocked is able to register that it was called when promise was resolved:
https://github.com/zulucoda/-jest-mock-promise-reject/blob/master/src/login.js#L43
dispatch(LoginLoaded(results));
https://github.com/zulucoda/-jest-mock-promise-reject/blob/master/src/login.test.js#L24
expect(dispatch.mock.calls[ 0 ][ 0 ]).toEqual(LoginLoading());
expect(dispatch.mock.calls[ 1 ][ 0 ]).toEqual(LoginLoaded({}));
Test 2 - reject promise - This fails
https://github.com/zulucoda/-jest-mock-promise-reject/blob/master/src/login.test.js#L35
service.onLogging = jest.fn(() => Promise.reject('some error'));
and what I mean by this fails is the dispatch mock calls does not recognise that it has been called when promise is rejected. But I can confirm that the promise rejects fine because the console.log is called in the catch function.
https://github.com/zulucoda/-jest-mock-promise-reject/blob/master/src/login.js#L44
https://github.com/zulucoda/-jest-mock-promise-reject/blob/master/src/login.js#L45
https://github.com/zulucoda/-jest-mock-promise-reject/blob/master/src/login.js#L46
dispatch(LoginError());
console.log('Promise RECT!!!!!', error);
@thymikee I logged this as a bug because I of the above explanation, maybe I'm not explaining this correctly, but if you would just kindly clone this test repo and just run the tests you will see what I'm talking about.
Hm, that's really strange.
Another curious thing about this โ Jest fails totally (I mean exits) when there is an error thrown inside mocked .catch() which is not caught later. Reminds me of this: https://github.com/facebook/jest/issues/2059
Reopening, sorry for closing too early!
@thymikee thanks mate for having a re-look at it. no worries about closing the bug early, i think it was my fault I didn't explain the problem correctly. Anyway I can imagine the amount of effort it takes to maintain an open source project, so the last thing I want to do is waste your time by logging something that wasn't a bug.
okay thanks again, i will monitor this thread #2059 to see how progress is going on resolution.
@zulucoda
It's a bug in your code ๐
You don't return a promise, and await ... doesn't wait for it.
dispatch(LoginError()) is invoked after the test finishes.
See log:

It passes if you add return here

@lsentkiewicz wow well done for solving this one, thanks again ๐
oh tho it's still strange that without the return there the first test still passes which is resolving the the promise instead of rejecting it.
service.onLogging = jest.fn(() => Promise.resolve({}));
anyway at least I can move on, thanks again for assisting me.
Most helpful comment
@zulucoda

It's a bug in your code ๐
You don't return a promise, and
await ...doesn't wait for it.dispatch(LoginError())is invoked after the test finishes.See log:
It passes if you add

returnhere