I'm trying to assert an error which is thrown async in an express middleware:
The middleware to test:
const request = require('request');
const middleware = function (options) {
if (!options) {
throw new Error('Options are missing.'); // gets catched
}
request(options.uri, (err, response) => {
if(err) {
throw err;
}
});
return function (req, res, next) {}
}
module.exports = middleware;
The mocha
test:
describe('middleware', () => {
describe('if async error is thrown', () => {
it('should return an error', done => {
try {
middleware({
uri: 'http://unkown'
});
} catch (err) {
assert.equal('Error: getaddrinfo ENOTFOUND unkown unkown:80', err.toString());
return done();
}
});
});
})
The problem is, that err
doesn't get catched inside the test:
Uncaught Error: getaddrinfo ENOTFOUND unkown unkown:80
at errnoException (dns.js:27:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:78:26)
I understand that it happens because the error is thrown async but I don't know how to work around it here.