Timeout and Promise causing "Uncaught Error" in console
See original GitHub issueI was unable to reproduce this issue in a command line project, but I was able to using the website HTML example: http://cucumber.github.io/cucumber-js/
I only changed the When
part in the sample code, but it happens on Given
and Then
as well:
this.When(/^I increment the variable by (\d+)$/, function(number, next) {
this.incrementBy(number);
setTimeout(() => {
throw 'this is an uncaught error'
next()
}, 1000)
});
Looking at the console when running the feature, I get the message:
Uncaught this is an uncaught error
It never goes to next()
, so it take 5 seconds to finish. The same issue happens in Promise.then
and also in setInterval
. I also tested when returning a Promise
and not passing next
, but experienced the same issue.
I use Chai as my assertion library, and when I do expect(true).to.be.false
, it throws and Assertion Error
and normally; that gets picked up through Cucumber which properly fails the test. On the other hand, if I wrap this error in a setTimeout
, setInterval
, or Promise
, then the Uncaught
error message shows up in the console instead of Cucumber picking up on the error.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (2 by maintainers)
Top GitHub Comments
I see, so I could do something like this:
And wrap my statement like so:
Or I could use the much easier method without
next
:I get it now. Cucumber has a promise handler which will catch any errors that I’m not already trying to catch in my Promise. I was able to break Cucumber’s error handling by adding a
.catch
to the promise:And that’s how it should work since I’m specifically catching this error before sending it to Cucumber. If I returned a
Promise.reject(err)
in the.catch
, then Cucumber could pickup that error as it’s now “uncaught” by my code:I also verified it works with jQuery’s legacy promise (before version 3):
Which means there’s a try-catch surrounding the execution of
this.Given
. So when you have an error in a setTimeout or Promise, there’s no way for Cucumber to know if there’s an exception thrown unless you explicitly tell it usingnext('Didn't pass assertion!')
, or you give it access to the promise pointer and let the try-catch in Cucumber handler it.Now I get it thanks!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.