thomas thomas - 1 year ago 177
Node.js Question

Mocha throws "Resolution method is overspecified"

I'm trying to write a simple test -

describe("My test", function(){
it("Succeed", function*(done){
yield testFunc();
done()
})
})

function *testFunc(){
console.log("Hey!")
}


Note that I'm using co-mocha so that my generator will be executed.

The error that I get is -

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


Now, the docs clearly state -


when a test injects a callback function (suggesting asynchronous execution), calls it, and returns a Promise, Mocha will now throw an exception


https://github.com/mochajs/mocha/blob/master/CHANGELOG.md#boom-breaking-changes

However, I'm not returning a Promise! I'm injecting the
done
function which is a callback, but I'm not returning a Promise... yielding testFunc doesn't return a Promise.

So, why is this test throwing an error?

Thanks

rsp rsp
Answer Source

The generator (function*) or rather it's coroutine-creating wrapper that's implicit here is most likely returning a promise. I'm not exactly sure how the co-mocha works here but maybe this would work:

describe("My test", function () {
  it("Succeed", function* () {
    yield testFunc();
    return;
  });
});

The return is not needed here, just added for clarity.

But you may need to change:

function *testFunc(){
  console.log("Hey!")
}

to something like:

let testFunc = co.wrap(function* () {
  console.log("Hey!");
});

for your yield in the code above to work.

If you're using co coroutines then your generators should yield promises. Here, you're generator yields the result of running a generator function that itself returns a generator, not a promise.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download