如何查看jasmine-node抛出的异常?

时间:2014-08-31 21:04:54

标签: node.js unit-testing jasmine jasmine-node

我在使用jasmine-node测试代码时遇到异常问题。我没试过--captureExceptions旗帜。

最小例子:

测试/ mySpec.js

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

测试/ badness.js

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

尝试运行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

我原本期望堆栈跟踪或指示 引发错误。这可能/例外吗?一个错误,或者我错误的期望?

1 个答案:

答案 0 :(得分:0)

由于JavaScript中的所有东西都取决于JavaScript引擎,我会说实现V8的大多数现代引擎都很酷,但有异常。试试这个:

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

此外,不要在控制台中查看,而是尝试使用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();
});
相关问题