done() called within superagent request not working

563 Views Asked by At

I am creating an app with a feature to post on login. I have written the following test code in mocha. I get this error:

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

I think I made some mistake in using done(). All other assert statements have passed. Let me know where I am going wrong, or if if you need more info.

it("Login and post", function(done) {
    superagent
        .post(URL_ROOT + "/register")
        .send({
            email: "[email protected]",
            password: "posttest"
        })
        .end(function(err, res) {
            assert.ifError(err);
            var token = res.body.token;
            superagent
                .post(URL_ROOT + "/post")
                .send({
                    content: "testing post",
                    user: jwt.decode(token).id
                })
                .set("Authorization", "test_scheme " + token)
                .end(function(err, res){
                    assert.ifError(err);
                    assert.equal(res.body.post.content, "testing post");
                    done();
                });
        });
});
1

There are 1 best solutions below

0
On

Your test might be taking more than 2 seconds to run: In the first line of your callback to it, right before superagent .post(URL_ROOT + "/register")... add this line:

this.timeout(30000)

This allows your to only time out after 30 seconds instead of 2.