简体   繁体   中英

How to see thrown exceptions with jasmine-node?

I'm having trouble seeing exceptions thrown when I test code with jasmine-node . I've tried the --captureExceptions flag with no luck.

Minimal example:

test/mySpec.js

var r = require('./badness.js')
describe("things:", function(){
  it("can", function(){
    expect(r()).toBe("work")
  })
})

test/badness.js

module.exports = function(){
  throw "badness";
  return "work";
};

Try running jasmine

npm install jasmine-node --save-dev
$ ./node_modules/.bin/jasmine-node --captureExceptions test/
F

Failures:

  1) things: can
   Message:
     badness
   Stacktrace:
     undefined

Finished in 0.004 seconds
1 test, 1 assertion, 1 failure, 0 skipped

I would have expected a stack trace or indication of where the error was thrown from. Is this possible / excepted? A bug, or my incorrect expectations?

As everything in JavaScript it depends on the JavaScript engine, I would say that most moderns engine implementing V8 are cool with Error exception. Try this:

throw new Error('Error with better stacktrace');

Also, instead of look in the console, try to use the Jasmine matcher "toThrow".

it("The 'toThrow' matcher is for testing if a function throws an exception", function() {
  var foo = function() {
    return 1 + 2;
  };
  var bar = function() {
    return a + 1;
  };

  expect(foo).not.toThrow();
  expect(bar).toThrow();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM