2

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?

Mohan Singh
  • 1,142
  • 3
  • 15
  • 30

2 Answers2

1

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)
})
Richard Price
  • 482
  • 4
  • 13
  • No. I added it inside the test. Does that not work? – Taksh Pratap Singh Jan 03 '20 at 11:40
  • Unfortunately, if the third argument that you pass is longer than the default timeout value (5000 ms), jest will simply ignore it and use the default value of 5000 ms. That is why, we need to use jest.setTimeout() to change the timeout value. – Taksh Pratap Singh Jan 03 '20 at 11:56
1

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).