Do you want to request a _feature_ or report a _bug_?
Bug
What is the current behavior?
Jest doesn't report an uncaughtException when it happens in a promise.
const EventEmitter = require('events');
// Succeed
it('should fail promise', () => {
const connection = new EventEmitter();
const fn = jest.fn(() => {
connection.emit('error', new Error());
return Promise.reject(new Error());
});
return Promise.resolve().then(fn).catch(() => {});
});
// Fails
it('should fail standard', () => {
const connection = new EventEmitter();
connection.emit('error', new Error());
});
What is the expected behavior?
Both tests should fail.
Promise.resolve does not execute functions passed to it. Try Promise.resolve().then(() => connection.emit('error', new Error())).
Sorry my reduction of the test case was not enough:
const EventEmitter = require('events');
// Succeed
it('should fail promise', () => {
const connection = new EventEmitter();
const fn = jest.fn(() => {
connection.emit('error', new Error());
return Promise.reject(new Error());
});
return Promise.resolve().then(fn).catch(() => {});
});
// Fails
it('should fail standard', () => {
const connection = new EventEmitter();
connection.emit('error', new Error());
});
EDIT: I'm opening a new issue with the correct test case.
Why did you closed both issues? The second test case provided shows the bug.
I have non testable code because some dependencies throw an error event AND a promise rejection.
How do you expect the promise to throw, if you catch it?
The issue is that an event emitter emitting an error event throws an uncaughtException at process level if there is no listener for the 'error' event. You can't catch it in your function.
In the first test, it should fail because there is an uncaught exception. It seems the promise rejecting cancel the output for jest, I don't know why but it makes code that uses amqplib untestable.
For reference, here is the Node documentation referring to why the test should fails:
https://nodejs.org/api/events.html#events_error_events
EDIT: Can we reopen the issue please ? It a serious issue to get a false positive on a test.
Can we reopen the issue please ? It a serious issue to get a false positive on a test.
Same problem. I haven't found a solution yet.
I think it works if you return the dispatch:
it('should fail when promise fail', () => {
const store = mockStore(initialState);
return store.dispatch(fetchSomething).then(() => {
expect(store.getActions()).toEqual(expectedActions);
expect(store.getState()).toEqual(expectedState);
});
});
Most helpful comment
Why did you closed both issues? The second test case provided shows the bug.
I have non testable code because some dependencies throw an error event AND a promise rejection.