【问题标题】:promise & mocha: done() in before or not?promise & mocha: done() in before or not?
【发布时间】:2018-01-16 07:17:55
【问题描述】:

我正在阅读some tutorials on promise tests in mocha。有一段代码:

before(function(done) {
  return Promise.resolve(save(article)).then(function() {
    done();
  });
});

为什么done()before() 中调用then()?上面的代码和下面的代码有什么区别:

before(function(done) {
  return Promise.resolve(save(article));
});

谢谢

更新

我的问题是与以下代码进行比较:

before(function() {
  return Promise.resolve(save(article));
});

抱歉打错了。

【问题讨论】:

    标签: javascript promise mocha.js


    【解决方案1】:

    在这种特殊情况下,没有功能差异。回调,通常称为done,是为了在使用回调时处理异步测试而引入的。返回一个Promise 就足够了,但请注意,您不能定义done 回调,因为测试套件会等到它被调用。当您无法轻松返回Promise 时,请使用done。在您的情况下,第二个测试将是无限的,因为您定义了 done,而您实际上从未调用过它。

    【讨论】:

      【解决方案2】:

      我不确定我是否 100% 理解了这个问题,但在调用 done 之前测试不会开始。

       beforeEach(function(done) {
          setTimeout(function() {
            value = 0;
            done();
          }, 1);
        });
      

      在上述对 beforeEach 的调用中调用 done 函数之前,该测试不会开始。在调用 done 之前,该规范不会完成。

        it("should support async execution of test preparation and expectations", function(done) {
          value++;
          expect(value).toBeGreaterThan(0);
          done();
        });
      

      您不必在示例中通过 done,只需:

      before(function() {
        return Promise.resolve(save(article));
      });
      

      如果你确实通过了done,测试运行器将在继续之前被调用,否则它可能会抛出超时错误。

      【讨论】:

        【解决方案3】:

        第一个带有before钩子的代码sn-p返回一个promise并且调用done。在 Mocha 3.x 及以上版本中,会导致此错误:

        Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.
        

        过去,如果你使用 done 并返回一个 Promise 并不重要,但最终 Mocha 开发人员认为同时指定 done 返回一个 Promise 只是意味着测试设计师犯了一个错误,最好让 Mocha 调整一下,而不是默默地允许它。

        在您的第二个 sn-p 中,您有 done 参数并返回一个承诺,但 Mocha 仍将等待 done 被调用并超时。 (它确实应该检测到参数并像第一种情况一样引发错误,但它没有......)

        一般来说,如果您正在测试一个产生 Promise 的异步操作,返回 Promise 比使用 done 更简单。下面是一个说明问题的示例:

        const assert = require("assert");
        
        // This will result in a test timeout rather than give a nice error
        // message.
        it("something not tested properly", (done) => {
            Promise.resolve(1).then((x) => {
                assert.equal(x, 2);
                done();
            });
        });
        
        // Same test as before, but fixed to give you a good error message
        // about expecting a value of 2. But look at the code you have to
        // write to get Mocha to give you a nice error message.
        it("something tested properly", (done) => {
            Promise.resolve(1).then((x) => {
                assert.equal(x, 2);
                done();
            }).catch(done);
        });
        
        // If you just return the promise, you can avoid having to pepper your
        // code with catch closes and calls to done.
        it("something tested properly but much simpler", () => {
            return Promise.resolve(1).then((x) => {
                assert.equal(x, 2);
            });
        });
        

        关于异步操作的完成,无论您使用itbeforebeforeEachafterafterEach,它的工作原理都是一样的,所以即使我给出的示例是@987654335 @,同样适用于所有的钩子。

        【讨论】:

          猜你喜欢
          • 2017-05-02
          • 2021-07-06
          • 2015-06-30
          • 1970-01-01
          • 1970-01-01
          • 2021-08-24
          • 2019-07-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多