-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
thrown errors inside server.inject does not propagate #1299
Comments
I don't know what is the state of mocha support for node domains. hapi uses a domains a lot. This was the main reason we moved away from mocha and are using lab instead. If you look at the hapi tests, we use chai which throws inside server.inject() all the time. |
No, don't close this issue! It has nothing to do with Mocha and if you take a second look at the example code I provided you will see that it does not contain any Mocah related code – it is pure Hapi. Thrown errors does not proppogate from inside |
This is odd. I can see the error if I wrap it in a domain: var Hapi = require('hapi');
var Domain = require('domain');
var domain = Domain.createDomain();
domain.run(function () {
var server = new Hapi.Server(8000);
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('hello world');
}
});
server.inject({ method: 'GET', url: '/' }, function (res) {
throw new Error('Never shown');
console.log(res.result);
});
});
domain.on('error', function (err) {
console.log(err.stack);
}); |
Disregard that. The initial code clearly shows there is also a problem in hapi, as well as elsewhere. |
To save people some searching, this is a mocha issue (and probably one w/ most other JS test frameworks). Mocha wraps all tests in a try/catch block which is what's causing this issue. The try/catch swallows the synchronous exceptions from assertions and those exceptions never makes it up to the domain's error handler. The try/catch is helpful for browser support, but with domains and Further reading: mochajs/mocha#513 |
@bendrucker if you run the code snippet I have provided in the first post – or read my second comment – you can see that this error is reproducible without requiring any modules except for |
So you're expecting the thrown exception to emit an |
I came to this issue after looking for a way to use mocha with hapi. require('should');
var Promise = require("bluebird"),
server = require('../');
function inject (options) {
return new Promise(function (resolve, reject) {
server.inject(options, resolve);
});
}
describe('api', function(){
it('should have this route', function () {
return inject({ method: "GET", url: "/api/missing_route"}).then(function (response) {
//this will throw an error
response.statusCode.should.eql(200);
});
});
}); |
@bendrucker Cool. I guess it's probably better to have this as a reusable plugin then what I did. |
The thrown error never appears in the console.
The text was updated successfully, but these errors were encountered: