node.js - Jasmine Node 异步不理解代码错误

标签 node.js jasmine-node

我正在尝试使用异步 Jasmine Node :

it('should not be dumb', function(done){
Promise.resolve(0).then(function(){
  console.log('dumb');
  expect(2).toEqual(2);
  done();
});

});

返回:

dumb
.

Finished in 0.026 seconds
1 test, 1 assertion, 0 failures, 0 skipped

但是不知何故,如果我的代码中有错误:

  it('should not be dumb', function(done){
  Promise.resolve(0).then(function(result){
    console.log('dumb');
    expect(2,toEqual(2));
    done();
    }, function (err){console.log(err); done()});
  });

它只是坐在那里直到超时,没有任何有用的输出:

dumb

Pairing user stories - 7816 ms
    should not be dumb - 7815 ms

Failures:

  1) Pairing user stories should not be dumb    Message:
     timeout: timed out after 5000 msec waiting for spec to complete    Stacktrace:
     undefined

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

我做错了什么?

最佳答案

假设:

promise.then(onResolve, onReject)
promise 被拒绝时,

onReject 将被调用。但是,在您的代码中,拒绝发生在 onResolve 中(隐式地,通过引发错误),这不会影响 promise (此时已经解决) ),因此不会调用 onReject

如果您想捕获 onResolve 中抛出的错误,您需要通过向您的 Promise 链添加另一个拒绝处理程序来处理它们:

Promise.resolve(0).then(function(result){
  console.log('dumb');
  expect(2,toEqual(2));
  done();
}).then(null, function (err) {
  console.log(err);
  done()
});

如果 Promise 恰好是 bluebird,您可以使用 .catch() 稍微缩短它:

Promise.resolve(0).then(function(result){
  console.log('dumb');
  expect(2,toEqual(2));
  done();
}).catch(function (err) {
  console.log(err);
  done()
});

关于node.js - Jasmine Node 异步不理解代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32385496/

相关文章:

javascript - Istanbul 尔没有运行所有 Jasmine 规范?

javascript - Jasmine-node - 在其他函数内部调用的构造函数上创建 spy

node.js - 在部署之前使用 AWS CodeBuild 或 Lambda 运行 jasmine 测试

node.js - 以太坊 Node 中的 web3(然后是属性)

ruby-on-rails - 在 ubuntu 上安装 Brunch

meteor - 在 Codeship 上使用 Velocity 测试 Meteor

javascript - Frisby ExpectJSON 包含无序列表

javascript - 永远的 Node.js 缓存问题

node.js - 在winston中记录每个请求的请求信息

javascript - 如何在 JavaScript 中获取标签的 ID