Artem Jackson
Artem Jackson

Reputation: 55

Using Q.js promise for unit-tests: timeout of 2000ms exceeded

I'm using Q.js library for simulating async behaviour by using promise

I have a stubed backend api

class ApiStub {
    constructor(){
        this.deferred = Q.defer();
    }

    post(url, data) {
        if (data) {
            this.deferred.resolve(data);
        } else {
            this.deferred.reject(new Error("Invalid data"));
        }
        return this.deferred.promise;
    }
}

and I'm trying to test it:

 before(() => api = new ApiStub());

 it("Api test", (done) => {
        return api.post({})
            .then(value => {
                expect(value).to.exist;
                done();
            }, error => { 
               done(error);
            });
 });

but I got an Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

I've tried to set mocha timeout more then 15000ms but it didn't helped

Upvotes: 0

Views: 248

Answers (1)

aebabis
aebabis

Reputation: 3705

It looks like you have your error handler as part of the the same then with your test case. This means you won't catch any errors thrown by the expect. Try this and see if you get a different error:

it("Api test", (done) => {
        return api.post({})
            .then(value => {
                expect(value).to.exist;
                done();
            }).catch(error => { 
                done(error);
            });
 });

Upvotes: 1

Related Questions