茉莉花节点异步不理解代码错误
jasmine-node async doesn't understand code errors
我正在尝试使用异步的 jasmine-node :
it('should not be dumb', function(done){
Promise.resolve(0).then(function(){
console.log('dumb');
expect(2).toEqual(2);
done();
});
});
returns :
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)
onReject
将在 promise
被拒绝时调用。但是,在您的代码中,拒绝发生在 onResolve
中(隐含地,通过抛出错误),这不会影响 promise
(此时已经解决)因此不会调用onReject
.
如果您想捕获 onResolve
中抛出的错误,您需要通过向您的承诺链添加另一个拒绝处理程序来处理它们:
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()
});
我正在尝试使用异步的 jasmine-node :
it('should not be dumb', function(done){
Promise.resolve(0).then(function(){
console.log('dumb');
expect(2).toEqual(2);
done();
});
});
returns :
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)
onReject
将在 promise
被拒绝时调用。但是,在您的代码中,拒绝发生在 onResolve
中(隐含地,通过抛出错误),这不会影响 promise
(此时已经解决)因此不会调用onReject
.
如果您想捕获 onResolve
中抛出的错误,您需要通过向您的承诺链添加另一个拒绝处理程序来处理它们:
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()
});