为什么我会收到"错误:解决方法被过度指定"?

cod*_*ire 32 mocha.js node.js

升级后,Mocha甚至无法运行这里的简单测试代码

const assert = require('assert');

it('should complete this test', function (done) {
  return new Promise(function (resolve) {
    assert.ok(true);
    resolve();
   })
  .then(done);
});
Run Code Online (Sandbox Code Playgroud)

我从这里拿了这个代码

我明白它现在抛出异常 Error: Resolution method is overspecified. Specify a callback * or * return a Promise; not both.

但是如何让它发挥作用?我不明白.我有

node -v 6.9.4

mocha -v 3.2.0
Run Code Online (Sandbox Code Playgroud)

如何运行此代码现在采用新的正确格式?

Igo*_*gor 35

刚落,
.then(done);并更换function(done)function()

你正在返回一个Promise,所以调用done是多余的,因为它在错误消息中说

在老版本中,你必须使用回调,以防异常方法

it ('returns async', function(done) {
   callAsync()
   .then(function(result) {
      assert.ok(result);
      done();
   });
})
Run Code Online (Sandbox Code Playgroud)

现在您可以选择返回Promise

it ('returns async', function() {
  return new Promise(function (resolve) {
     callAsync()
       .then(function(result) {
          assert.ok(result);
          resolve();
       });
  });
})
Run Code Online (Sandbox Code Playgroud)

但使用两者都是误导性的(例如参见https://github.com/mochajs/mocha/issues/2407)


Sim*_*ias 6

Mocha允许使用回调:

it('should complete this test', function (done) {
  new Promise(function (resolve) {
    assert.ok(true);
    resolve();
   })
  .then(done);
});
Run Code Online (Sandbox Code Playgroud)

返回承诺:

it('should complete this test', function () {
  return new Promise(function (resolve) {
    assert.ok(true);
    resolve();
   });
});

// Or in the async manner
it('should complete this test', async () => {
    await Promise.resolve();
    assert.ok(true);
});
Run Code Online (Sandbox Code Playgroud)

你不能两者都做。


khe*_*ngz 5

我不得不done从函数参数和done()函数调用 之前删除

   before(async function (done) {
        user = new User({ ...});
        await user.save();
        done()
    });
Run Code Online (Sandbox Code Playgroud)

   before(async function () {
        user = new User({ ...});
        await user.save();
    });
Run Code Online (Sandbox Code Playgroud)

这些对我有用