jasmine-node
jasmine-node copied to clipboard
Tests silently fail when exception thrown inside request callback.
var request = require('request');
describe("metric/series",function() {
it("should have expected structure (2)", function(done) {
request("http://example.com", function(error, response, body){
//throw "exception"
done();
});
});
});
This test passes. But if you uncomment throw "exception"
it fails silently.
try --captureExceptions
I tried it. The exception is captured and the word "exception" is printed to the output. However I still really need to know which tests caused the exception and which preferably even what statement caused it. Right now I'm wrapping possible places where exceptions like this could occur with:
var request = require('request');
describe("metric/series",function() {
it("should have expected structure (2)", function(done) {
request("http://example.com", function(error, response, body){
try {
throw "bananas"
} catch(err) {
expect(""+err).toBeUndefined();
}
done();
});
});
});
This is hacky, but I know the exact code that caused the problem.
Yeah, that's not a real workaround, this seems to be something to do with an exception within the request module. I don't think I've ever had an exception within an async test using either done
or waitsFor
/runs
that didn't fail properly. I wonder if there's a way to fail better within that request module... For reference, when the test isn't within request, you get something like this:
describe("metric/series",function() {
it("should have expected structure (1)", function() {
runs(function(){throw "exception"});
waitsFor(function(){return false;}, 'the impossible', 500);
});
it("should have expected structure (2)", function(done) {
throw "exception";
done();
});
});
~/G/jasmine-node:[master]✑ ./bin/jasmine-node aSpec.js --captureExceptions
FF
Failures:
1) metric/series should have expected structure (1)
Message:
exception
Stacktrace:
undefined
2) metric/series should have expected structure (1)
Message:
timeout: timed out after 500 msec waiting for the impossible
Stacktrace:
undefined
3) metric/series should have expected structure (2)
Message:
exception
Stacktrace:
undefined
Finished in 0.559 seconds
2 tests, 3 assertions, 3 failures, 0 skipped
without --captureException,
process.on('uncaughtException', function() { console.log('error'); });
after 5sec, you'll get time out and test result in your console.
- /Account sss Message: timeout: timed out after 5000 msec waiting for spec to complete Stacktrace: undefined
I don't know how and why it works, it just worked.
Can anyone explain for me?