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

时间:2022-09-04 08:02:21

After the upgrade, Mocha can not even run a simple test here is the code

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

const assert = require('assert');

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

I took this code from here

我从这里拿了这个代码

I understood that it now throws an exception Error: Resolution method is overspecified. Specify a callback * or * return a Promise; not both.

我明白它现在抛出一个异常错误:解决方法被过度指定。指定回调*或*返回Promise;不是都。

But how to make it work? I did not understand. I have

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

node -v 6.9.4

mocha -v 3.2.0

How to run this code are now in a new and correct format?

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

2 个解决方案

#1


12  

Just drop
.then(done); and replace function(done) with function()

然后放弃。然后(完成);并用函数()替换函数(完成)

You are returning a Promise so calling done is redundant as it said in error message

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

In the elder versions you had to use callback in case of async methods like that

在旧版本中,如果是异步方法,则必须使用回调

it ('returns async', function(done) {
   callAsync()
   .then(function(result) {
      assert.ok(result);
      done();
   });
})

Now you have an alternative of returning a Promise

现在您可以选择返回Promise

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

But using both is misleading (see for example here https://github.com/mochajs/mocha/issues/2407)

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

#2


3  

Mocha allows to either use a callback:

Mocha允许使用回调:

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

OR return a promise:

或者回复承诺:

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

You can't do both.

你不能两者都做。

#1


12  

Just drop
.then(done); and replace function(done) with function()

然后放弃。然后(完成);并用函数()替换函数(完成)

You are returning a Promise so calling done is redundant as it said in error message

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

In the elder versions you had to use callback in case of async methods like that

在旧版本中,如果是异步方法,则必须使用回调

it ('returns async', function(done) {
   callAsync()
   .then(function(result) {
      assert.ok(result);
      done();
   });
})

Now you have an alternative of returning a Promise

现在您可以选择返回Promise

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

But using both is misleading (see for example here https://github.com/mochajs/mocha/issues/2407)

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

#2


3  

Mocha allows to either use a callback:

Mocha允许使用回调:

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

OR return a promise:

或者回复承诺:

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

You can't do both.

你不能两者都做。