首页 文章

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

提问于
浏览
1

当我用 jasmine-node 测试代码时,我遇到了抛出异常的问题 . 我没试过 --captureExceptions 旗帜 .

最小的例子:

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";
};

尝试运行茉莉花

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

    作为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();
    });
    

相关问题