Closed
Description
I stumbled on a problem with how lab treats errors thrown in the context of a promise chain, but that aren't promise rejections due to being thrown from a separate function stack because of asynchronicity.
It's easier to explain with an example. Here's a failing test:
const Lab = require('lab')
const lab = exports.lab = Lab.script()
lab.test('uncaught exception from promise test', (done) => {
Promise.resolve()
.then(() => {
setImmediate(() => {
throw new Error('test error')
})
})
.catch((err) => {
// Code never reached (as expected, since the thrown error above is not a promise rejection)
console.log('handled promise rejection!', err)
done(err)
})
})
lab.test('second test', (done) => {
// Code never reached... Unless we add an 'uncaughtException' handler at the process level,
// in which case the first case will eventually timeout, which is still undesirable
console.log('second test started')
done()
})
The error is simply not handled by lab and the process crashes in the first test.
If we add an 'uncaughtException' handler at the process level, the crash is prevented and the first test will eventually timeout, and then the second test will be executed. However, having to wait for the timeout is not desirable.
Lab should handle uncaught exceptions regardless of where they are thrown from.