thomas thomas - 4 years ago 289
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();

function *testFunc(){

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

However, I'm not returning a Promise! I'm injecting the
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?


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

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

But you may need to change:

function *testFunc(){

to something like:

let testFunc = co.wrap(function* () {

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