Code-1:
describe("testing server functionalities", () => {
// test case
it("server handles a function", () => {
request('some link', (error, response, body) => {
var expected = "1410";
expect(expected).to.be.equal(body);
})
})
});
Code-2:
describe("testing server functionalities", () => {
// test case
it("server handles a function", (done) => {
request('some link', (error, response, body) => {
var expected = "1410";
expect(expected).to.be.equal(body);
done();
})
})
});
So here in Code-1
, we have the problem that the test exits before checking whether the value
meets the expected value
because the request call is asynchornous.
To handle this problem, I found a way where the introduced argument done
as in the Code-2
makes the test function wait for the done()
function to get called which finally makes the test function work properly.
But I am not sure how the Code-2
works and why does the test function wait for the done
.
Can someone please help me with this?