承诺与摩卡:完成()之前与否? [英] promise & mocha: done() in before or not?

查看:61
本文介绍了承诺与摩卡:完成()之前与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一些关于摩卡中的承诺测试的教程。有一段代码:

I am reading some tutorials on promise tests in mocha. There is a piece of codes:

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

为什么 done() then()之前()?上述代码与以下代码有什么区别:

Why done() called in the then() in the before()? What is the difference between the above codes and the following codes:

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

谢谢

UPDATE

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

My question is to compare with the following codes:

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

很抱歉打字错误。

推荐答案

在挂钩之前带有的第一个代码片段返回一个promise 调用 done 。在Mocha 3.x及更高版本中,它将导致此错误:

The first code snippet with the before hook returns a promise and calls done. In Mocha 3.x and over, it will result in this error:

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

过去,如果你使用 done <并不是特别重要/ code>并返回了一个承诺,但最终Mocha开发人员认为指定完成 返回一个承诺只是意味着测试设计师犯了一个错误,最好让摩卡音调适合而不是默默地允许它。

It used to be that it did not particularly matter if you used done and returned a promise, but eventually the Mocha devs figured that specifying both done and returning a promise just meant the test designer made a mistake and it was better to have Mocha pitch a fit rather than silently allow it.

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

In your 2nd snippet, you have the done argument and return a promise but Mocha will still wait for done to be called and will timeout. (It really should detect the argument and raise an error like in the 1st case, but it doesn't...)

一般来说,如果你正在测试一个产生promise的异步操作,返回promise比使用 done 更简单。这是一个说明问题的例子:

Generally, if you are testing an asynchronous operation that produces a promise, it is simpler to return the promise than use done. Here's an example illustrating the problem:

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

关于异步操作的完成,无论你使用<$ c $,它都是一样的c> 之前 beforeEach 之后 afterEach 所以尽管我给出的例子是,但同样适用于所有钩子。

With regards to the completion of asynchronous operations, it works the same whether you are using it, before, beforeEach, after or afterEach so even though the example I gave is with it, the same applies to all the hooks.

这篇关于承诺与摩卡:完成()之前与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆