Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 369
coder fire Avatar asked Jan 20 '17 10:01

coder fire


4 Answers

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)

like image 106
Igor Popov Avatar answered Nov 07 '22 17:11

Igor Popov


Mocha allows to either use a callback:

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.

like image 25
Simon Boudrias Avatar answered Nov 07 '22 19:11

Simon Boudrias


I had to removed the done from the function parameter and the done() of the function call Before

   before(async function (done) {
        user = new User({ ...});
        await user.save();
        done()
    });

After

   before(async function () {
        user = new User({ ...});
        await user.save();
    });

These works for me

like image 26
kheengz Avatar answered Nov 07 '22 18:11

kheengz


I had this same issue. A lot of times Mocha is paired with another library called Chai. Chai has a package called "chai-as-promised". It gives you the super simple ability to write less code and test promises. In your case of just testing if a promise resolves, it seems perfect.

const chai = require('chai');
const chaiAsPromised = require("chai-as-promised");
const should = require("chai").should();
chai.use(chaiAsPromised);

describe("Testing with correct syntax and non repeated names", () => {
    it("Should give us a positive response", () => {
      graphQL.sendToGQL(model,"specialEndpoint").should.eventually.be.an("Object");
    })
})

like image 3
Justin Rice Avatar answered Nov 07 '22 19:11

Justin Rice