Search code examples
node.jsmocha.jschaichai-http

Not able to find Done in mocha test cases using Chai-http


I'm learning to write test cases using mocha and chai for the node application,I had written the below test case

let chai = require('chai');
let chaiHttp = require('chai-http');

const should = chai.should;
const expect = chai.expect;
const server = "http:\\localhost:3000"

chai.use(chaiHttp);

describe('Create Login and Register', () => {
    it('should login using credentials', () => {
        chai.request(server)
            .get('/register')
            .send()
            .then((res: any) => {
                res.should.have.status(200);
                done();
            }).catch((err: any) => { done(err) })
    })

})

but it shows the read swiggle below the done(); function
enter image description here

do i need to add some types for it to get working what is that i'm missing ,i tried to install chai-http again but still same issue


Solution

  • done is passed in as the first argument to your test function.

    describe('Create Login and Register', () => {
        it('should login using credentials', (done) => { // <-- here
            chai.request(server)
                .get('/register')
                .send()
                .then((res: any) => {
                    res.should.have.status(200);
                    done();
                }).catch((err: any) => { done(err) })
        })
    })
    

    Although, because you are using a Promise chain, you should just return the chain instead.

    describe('Create Login and Register', () => {
        it('should login using credentials', () => {
            return chai.request(server)
                .get('/register')
                .send()
                .then((res: any) => {
                    res.should.have.status(200);
                }); // a rejected promise will fail the test automatically
        })
    })