I am using Mocha and Chai to test my Node/Express API, and I can't figure out why the test is not reaching the .end()
Here is the test:
it('should authenticate successfully with user credentials', function (done) {
agent
.post('/login')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({ 'username': 'username', 'password': 'password'})
.end(function (err, res) {
console.log(res);
console.log('***************************Authenticated*********************************************');
expect(res).to.have.status(200);
});
done();
});
And here is the route I am hitting:
app.post('/login', passport.authenticate('ldapauth', { successRedirect: '/' }));
I figure my problem may be with the fact that there is no formal response, but rather a redirect, but I am not sure how to handle it.
The solution ended up being to move the done() callback into my .end() method. Thanks @robertklep
If you are testing async methods int mocha, you should call call method in callback function as below.
it('should authenticate successfully with user credentials', function (done) {
agent
.post('/login')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({ 'username': 'username', 'password': 'password'})
.end(function (err, res) {
console.log(res);
console.log('***************************Authenticated*********************************************');
expect(res).to.have.status(200);
done();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With