0

I am trying to use a setTimeout callback function in my unit tests. Basically, after executing a test, it needs to wait for 2 seconds before it can execute the following tests. However, I get this error while executing:

  Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

Here is my code:

function delay(seconds,callback) {
    setTimeout(callback, seconds*1000);
}

describe('/REGISTER users', () => {
    it('should not REGISTER a user without a fullname field', (done) => {
            console.log('Starting delays');
            delay(2, () => {
                console.log('Two seconds delayed');
                let user = {
                    email: 'nilesh_maharjan@gmail.com',
                    password: 'abcdefghij'
                }
                chai.request(server)
                .post('/api/register')
                .send(user)
                .end((err, res) => {
                    res.should.have.status(422);
                    res.body.should.be.a('object');
                    res.body.should.not.have.property('fullname');
                    done(); 
                });
            });           
    });

    it('should REGISTER a user', (done) => {
        console.log('Starting delays');
        delay(2, () => {
            console.log('Two seconds delayed');
            let user = {
                email: 'mjn.nileshabcd@gmail.com',
                password: 'abcdefghij',
                fullname: 'Nilesh Maharjan'
            }
            chai.request(server)
            .post('/api/register')
            .send(user)
            .end((err, res) => {
                res.should.have.status(201);
                res.body.user.should.have.property('email');
                done();
            });
        });        
    });
});

0 Answers0