Search code examples
redispromisemocha.jsbluebirdsupertest

Testing with supertest and mocha fails to resolve promise


I'm testing my rest application (made with hapi on node) with mocha (3.2) and supertest (3.0) using promises.

It stops after timeout and returns error:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

I already tried to increment timeout but it didn't work.

If I add a done() call I get:

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

Could you help me please?

This is my test:

...
const request = require('supertest');
const redis = require('redis');
const bluebird = require("bluebird");
bluebird.promisifyAll(redis.RedisClient.prototype);
const config = require('../src/config/tests');

describe('Routing', function () {

    let url = 'http://localhost:8080';

    let storage = redis.createClient({
        host: config.config.host,
        port: config.config.port
    });

    it('should pass', function (done) {
        let username = 'user',
            userHash = md5(username),
            data = {
                user_id: username,
                sess_id: 'session'
            };
        return storage.delAsync("users:" + userHash)
            .then(result => {
                return request(url)
                    .post('/login')
                    .send(data)
                    .expect(201)
                    .expect({info: true});
            })
            .then(response => {
                return storage.hgetallAsync("users:" + userHash);
            })
            .then(result => {
                assert({user_id: userHash}, result);
            })
    });

});

Solution

  • Specifying done parameter in function signature marks the test as asynchronous. And not calling it results in pending test.

    If it is specified with function (done) { ... }, it should be called. This is what the error says. And it shouldn't be specified if the spec was made asynchronous by returning a promise.