Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chai not reaching .end()

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.

like image 779
Jk Jensen Avatar asked Oct 12 '25 11:10

Jk Jensen


2 Answers

The solution ended up being to move the done() callback into my .end() method. Thanks @robertklep

like image 66
Jk Jensen Avatar answered Oct 14 '25 23:10

Jk Jensen


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();
            });

    });
like image 26
Jagajit Prusty Avatar answered Oct 15 '25 01:10

Jagajit Prusty