Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

node.js - Why am I getting "Error: Resolution method is overspecified"?

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

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...