3

when i run my function it gives me error : Resolution method is overspecified. Specify a callback *or* return a Promise; not both. can anyone please check my code, and help to resolve this issue ? here i have placed my full code

it('Get Organizations', async function (done) {
        let user_permission = await commonService.getuserPermission(logged_in_user_id,'organizations','findAll');
        let api_status = 404;
        if(user_permission) { 
            api_status = 200;
        }

        let current_token = currentResponse['token'];
        old_unique_token = current_token;

        chai.request(app)
            .post('entities')
            .set('Content-Type', 'application/json')
            .set('vrc-access-token', current_token)
            .send({ "key": "organizations", "operation": "findAll" })
            .end(function (err, res) {
                currentResponse = res.body;
                expect(err).to.be.null;
                expect(res).to.have.status(api_status);
                done();
            }); 

    });
Nikul Panchal
  • 1,542
  • 3
  • 35
  • 58

2 Answers2

6

Don't use done when defining an async unit test function, rather simply return as you normally would from an async method, after awaiting the request from chai-http:

it('Get Organizations', async function () {
        let user_permission = await commonService.getuserPermission(logged_in_user_id,'organizations','findAll');
        let api_status = 404;
        if(user_permission) { 
            api_status = 200;
        }

        let current_token = currentResponse['token'];
        old_unique_token = current_token;

        await chai.request(app) // note 'await' here
            .post('entities')
            .set('Content-Type', 'application/json')
            .set('vrc-access-token', current_token)
            .send({ "key": "organizations", "operation": "findAll" })
            .then(function (err, res) { // not 'then', not 'end'
                currentResponse = res.body;
                expect(err).to.be.null;
                expect(res).to.have.status(api_status);
            }); 

    });

The test runner will await your function automatically.

Myk Willis
  • 12,306
  • 4
  • 45
  • 62
  • if i don't use done(), then it gives me this error : Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; – Nikul Panchal Oct 14 '19 at 15:15
  • Ah, it seems chai.request() might require that you use `then()` instead of `end()` to return a Promise. Look at "Dealing with the response - Promises" example here: https://www.chaijs.com/plugins/chai-http/ – Myk Willis Oct 14 '19 at 15:22
-1

It is an "issue" with mocha. Remove the done() from your code. It is redundant when using Promises like in chai.request.

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

From: this stack

CHBresser
  • 185
  • 2
  • 2
  • 15