Search code examples
node.jsamazon-dynamodbjestjsamazon-snssupertest

How to add delay before/during test (using supertest & jest) while waiting for dynamodb event to occur


I'm writing tests for a Nodejs REST app in supertest & jest, and can't figure out how to add a delay between two tests being executed.

I have two tests in sequence; the first calls an endpoint that sends an SNS message to DynamoDB, and the second checks that this message has arrived and saves some of the details for future tests. The second often fails because the SNS message hasn't had sufficient time to reach DynamoDB. I simply want to add some kind of delay or wait before the second test runs to accommodate this.

The two tests are as follows:

describe('User1 invite User2', () => {
    it('It should return OK and send message to User2', () => {
        return request.post('/room/' + room_id + '/invitation')
        .query({zone_id: zoneid, session_id: user1_session_id, user_id: user2_userid, token: room_token})
        .set('Authorization', user1_id_token)
        .expect(200)
    })
})


describe('User2 get invite message', () => {
    it('It should return OK with message details: room_id, room_token, user1_userid', () => {
        return request.get('/lobby/mailbox')
        .query({ session_id: user2_session_id })
        .set('Authorization', user2_id_token)
        .expect(200)
        .then((response) => {
            received_room_id = response.body.mailbox_message.room_id;
            received_room_token = response.body.mailbox_message.room_token;
            from_user = response.body.mailbox_message.sender_user_id;
            expect(received_room_id).toEqual(room_id);
            expect(from_user).toEqual(user1_userid);
        })
    })
})

I've tried changing the second test to include async/await like below but not sure if I'm missing something as it doesn't seem to be working.

describe('User2 get invite message', () => {
    it('It should return OK with message details: room_id, room_token, user1_userid', async () => {
        const response = await request.get('/lobby/mailbox')
            .query({ session_id: user2_session_id })
            .set('Authorization', user2_id_token)
            .expect(200);
        received_room_id = response.body.mailbox_message.room_id;
        received_room_token = response.body.mailbox_message.room_token;
        from_user = response.body.mailbox_message.sender_user_id;
        expect(received_room_id).toEqual(room_id);
        expect(from_user).toEqual(user1_userid);

    })
})

I've also tried including .setTimeout() in the request but that doesn't seem to work either.

Still relatively new to nodejs so apologies in advance!


Solution

  • It's usually a bad practice to have one test rely on the output of another test - in this case, you can't run the second test in isolation.

    What about combining the tests into a single test? You could either put some Promise based delay handler or some application-specific polling mechanism in between the separate steps to guarantee the functionality.