Home > OS >  Unable to use a promise correctly when working with mocha: Error - Specify a callback *or* return a
Unable to use a promise correctly when working with mocha: Error - Specify a callback *or* return a

Time:12-02

I am unable to use a promise correctly when working with mocha (command : mocha --reporter spec --recursive --timeout 60000)

Getting errors like :

Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.
Error: Timeout of 60000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\GB\Documents\projects\requireurl\concurrency\test\test_demos_cluster.js)        
          at listOnTimeout (node:internal/timers:564:17)

const expect = require('chai').expect;

describe('test-.mjs::concurrency.js: Test Suite for concurrency.js Files', function () {
    var result
    before(async function (done) {
        function testPromise() {
            return new Promise(function (resolve, reject) {
                resolve({ msg: "testing" });
            })
        }
        result = await testPromise(); // return a promise with result
        done()
    });
    describe('test-.js::concurrency.js: [Test A] Test Suite for concurrency.js in main repo directory', function () {
        it('[Test A] Test for ', function (done) {
            // expect(100).to.equal(100);
            expect(result.msg).to.equal("testing");
            done();
        });
    });
});

CodePudding user response:

The error is self-explanatory:

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

Either use async or done

before(async function () {
        function testPromise() {
            return new Promise(function (resolve, reject) {
                resolve({ msg: "testing" });
            })
        }
        result = await testPromise(); // return a promise with result
    });

or

before(function (done) {
        function testPromise() {
            return new Promise(function (resolve, reject) {
                resolve({ msg: "testing" });
            })
        }
        testPromise().then(() => done());
    });
  • Related