jasmine-node icon indicating copy to clipboard operation
jasmine-node copied to clipboard

Tests silently fail when exception thrown inside request callback.

Open JnBrymn opened this issue 10 years ago • 4 comments

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.

JnBrymn avatar Mar 18 '14 01:03 JnBrymn

try --captureExceptions

tebriel avatar Mar 29 '14 14:03 tebriel

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.

JnBrymn avatar Mar 29 '14 15:03 JnBrymn

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

tebriel avatar Mar 29 '14 16:03 tebriel

without --captureException,

process.on('uncaughtException', function() { console.log('error'); });

after 5sec, you'll get time out and test result in your console.

  1. /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?

jongminjang avatar Jan 27 '15 07:01 jongminjang