Reputation: 3
I am trying to understand the use case of end()
function while writing the API test script in mocha and Chai. Also at the same time, I am confused about whether I shall use the done()
function here or not and also what is the exact difference between .end()
and .done()
.
Here's the code:
describe("Suite", () => {
it('Post Test case', (done) => {
request('https://reqres.in')
.post('/api/users')
.send({
"name": "morpheus",
"job": "leader"
})
.set('Accept', 'application/json')
.expect(200,'Content-Type', /json/)
.then((err,res) => {
console.log(JSON.stringify(err))
console.log(JSON.stringify(res.body))
console.log(JSON.stringify(" "))
})
done();
});
it('Put Test case', (done) => {
request('https://reqres.in')
.put('/api/users/2')
.send({
"name": "morpheus",
"job": "zion residents"
})
.expect(200)
.end((err, res) => {
console.log(JSON.stringify(err))
console.log(JSON.stringify(res.body))
console.log(JSON.stringify(" "))
})
done();
})
})
Upvotes: 0
Views: 421
Reputation: 8091
You are mixings things a little bit.
end
is a method of the expressjs framework and it ends the server response.
done
is a parameter of mocha test function. You call this parameter when you are finished with your asynchronous test to let the mocha
know that your asynchronous code is done executing, and it can move on to another test.
And in your case, you need them both.
Upvotes: 1