lundi 6 août 2018

Mocha promise test timout

I have the following test code in NodeJS:

'use strict';

// Example class to be tested
class AuthController {

    constructor() {
        console.log('Auth Controller test!');
    }

    isAuthorizedAsync(roles, neededRole) {
        return new Promise((resolve, reject) => {
            return resolve(roles.indexOf(neededRole) >= 0);
        });
    }
}

module.exports = AuthController;

And the following mocha test code:

describe('isAuthorizedAsync', () => {
    it('Should return false if not authorized', function(done) {
        this.timeout(10000);    

        authController.isAuthorizedAsync(['user'], 'admin')
            .then(isAuth => {
                assert.equal(true, isAuth);
                this.done();
            })
            .catch(err => {
                throw(err);
                done();
            });
    });    
});

which throws the following error:

 1 failing

1) AuthController
    isAuthorizedAsync
      Should return false if not authorized:
  Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (c:\supportal\node_modules\ttm-module\test\controllers\auth.controller.spec.js)

I have tried increasing the default mocha test timeout to 10secs and made sure that the promise resolves. I am new to mocha. Am I missing something here?

Aucun commentaire:

Enregistrer un commentaire