Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest how to test express API POST request?

I need to test if my POST request to my endpoint works properly with a Jest test. I had the idea of first getting the count of my Services table (I'm using sequelize orm), then to send a new post request and to finally get the new count and compare if the old count + 1 will equal to the new count, if true then the POST request works just fine.

test('Create a valid Service', async (done) => {
const service = {
    name: "cool",
    description: "description"
};

await Service.count().then(async function (count) {

    await request(app)
        .post('/api/services')
        .send(service)
        .then(async () => {
            await Service.count().then(function (newcount) {
                expect(newcount).toBe(count + 1);
            });
        })
        .catch(err => console.log(`Error ${err}`));
});

});

For me the test looks fine, but when I run it I get:

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

Is something missing or is there even a better way to test a POST request? with Jest?

like image 740
user9132502 Avatar asked Jan 22 '19 19:01

user9132502


1 Answers

It is because you are not calling the done callback passed in jest callback function. It can be done like this.

test('Create a valid Service', async(done) => {
    const service = {
        name: "cool",
        description: "description"
    };

    await Service.count().then(async function (count) {

        await request(app)
            .post('/api/services')
            .send(service)
            .then(async() => {
                await Service.count().then(function (newcount) {
                    expect(newcount).toBe(count + 1);
                    // execute done callback here
                    done();
                });
            })
            .catch(err => {
                // write test for failure here
                console.log(`Error ${err}`)
                done()
            });
    });
});

You can also write this code in this way so that the readability can be improved and maximize the use of async/await.

test('Create a valid Service', async(done) => {
    const service = {
        name: "cool",
        description: "description"
    };
    try {
        const count = await Service.count();
        await request(app).post('/api/services').send(service)
        const newCount = await Service.count()
        expect(newCount).toBe(count + 1);
        done()
    } catch (err) {
        // write test for failure here
        console.log(`Error ${err}`)
        done()
    }
});

By default Jest also resolves the promise in async/await case. We can achieve this without the callback function also

test('Create a valid Service', async() => {
    const service = {
        name: "cool",
        description: "description"
    };
    try {
        const count = await Service.count();
        await request(app).post('/api/services').send(service)
        const newCount = await Service.count()
        expect(newCount).toBe(count + 1);
    } catch (err) {
        // write test for failure here
        console.log(`Error ${err}`)
    }
});
like image 72
Supermacy Avatar answered Oct 23 '22 12:10

Supermacy