Taksh Pratap Singh
Taksh Pratap Singh

Reputation: 183

How to test express app with jest when you get an async timeout error?

I'm testing the following endpoint:

app.post('/api/master/makeLeaderInZone', authenticateMaster, (request, response) => {
    var body = _.pick(request.body, ['zoneId', 'leaderId'])

    Zone.findById(body.zoneId).then((zone) => {
        if (zone) {
            return zone.addLeader(body.leaderId)
        } else {
            return Promise.reject()
        }
    }).then(() => {
        return Volunteer.findById(body.leaderId)
    }).then((volunteer) => {
        if (volunteer) {
            return volunteer.addToZoneAsLeader(body.zoneId)
        } else {
            return Promise.reject()
        }
    }).then(() => {
        response.status(200).send()
    }).catch((error) => {
        response.status(400).send()
    })
})

And this is the test I am using:

describe('makeLeaderInZone', () => {
    test('neither zone nor volunteer valid',  () => {
         request(app)
        .post('/api/master/makeLeaderInZone')
        .set('master-auth', 'master')
        .send().expect(400)
    })
})

This endpoint worked perfectly when I tested it using postman. However, with this test, I got the following error:

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

As mentioned in this answer, I added the following line of code inside my test to make jest wait longer for my request to complete:

jest.setTimeout(30000);

But, I still got the exact same error. It's as if jest ignores this line and only waits for 5000ms.

What should I do to make this test work?

Upvotes: 2

Views: 2942

Answers (2)

Taksh Pratap Singh
Taksh Pratap Singh

Reputation: 183

I finally figured it out. I don't know why but for some reason, if I put the following line of code:

jest.setTimeout(30000);

inside the test function itself, jest just ignores it and uses the default timeout values of 5000 ms. But if I put this line inside the describe block but outside the test itself, like this:

describe('makeLeaderInZone', () => {
    jest.setTimeout(10000)

    test('neither zone nor volunteer valid', async () => {
        await request(app)
        .post('/api/master/makeLeaderInZone')
        .set('master-auth', 'master')
        .send().expect(400)
    })
})

it works perfectly and waits for my request to complete (which takes roughly 8.5 seconds).

Upvotes: 1

Richard Price
Richard Price

Reputation: 490

Just to confirm, did you add the setTimeout option to your jest.setup.js file as referenced here?

https://jestjs.io/docs/en/configuration#setupfilesafterenv-array

in order to set it on a test by test basis, you need to set it as a third argument to your test.

test('example', async () => {
  await new Promise(resolve => setTimeout(resolve, 1000));
}, 500);

So in your instance, it would be

describe('makeLeaderInZone', () => {
    test('neither zone nor volunteer valid',  () => {
         request(app)
        .post('/api/master/makeLeaderInZone')
        .set('master-auth', 'master')
        .send().expect(400)
    }, 30000)
})

Upvotes: 1

Related Questions