javascript - Jasmine 测试中 Bluebird promise 的警告

标签 javascript node.js promise bluebird

我已阅读有关 Promises not returning from handlers 的新警告并提出了相关问题...

在我的一些单元测试中,我使用 Jasmine 的 spy 功能来消除某些依赖项的功能以返回 promise 。因此,通过这一新的更改,我在测试运行时看到了很多警告。我当然可以禁用这些警告,但我想知道是否可以使用一种改进的方法来自然地消除此类错误?

示例:

beforeEach(function (done) {

  var formatter = new Formatter();
  var promise = Promise.resolve(['1,000', '2.45']);

  spyOn(formatter, 'format').and.returnValue(promise);

  // internally calls formatter.format()
  doStuff(formatter, [1000, 2.4567]).then(done);

 // promise is not returned
});

如果有用的话可以添加一个完整的工作插件吗?

最佳答案

问题在于 jasmine does notdone 回调返回任何内容,因此当您执行 .then(done) 时,您会触发 bluebird 的 a promise was created in a handler but none were returned from it 警告.

我通过向 Promise 对象添加一个新方法解决了这个问题:

var Promise = require('bluebird');

Promise.prototype.jasmineDone = function(done) {
    return this.then(function() {
        done();
        return null;
    }, function(err) {
        done.fail(err);
        return null;
    });
};

然后我重构我的规范(这会触发警告):

 it('should ...', function(done) {
    doSomething().then(done, done.fail);
 });

致:

 it('should ...', function(done) {
    doSomething().jasmineDone(done);
 });

关于javascript - Jasmine 测试中 Bluebird promise 的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35287107/

相关文章:

javascript - 跑道 JS : Cannot successfully subscribe to push service

javascript - 将文本文件读入 Typescript 中的对象数组中

node.js - 无法从Docker Registry Remote API获得有效响应

node.js - MongoDB Promise 和结果处理

javascript - 显示对象数组上的动态选择项映射

node.js - ResolveJS 后端系统的生产部署/运行时架构是什么?

javascript - 使用 Telnet 的 Node.js 客户端和服务器游戏

javascript - 一起使用 async await 和 .then

javascript - 最外面的 .catch() 是否适用于所有链式/嵌套式 Promise?

javascript - 嵌套for循环javascript的奇怪结果