【问题标题】:Jest how to test express API POST request?Jest 如何测试 Express API POST 请求?
【发布时间】:2019-06-16 08:04:24
【问题描述】:

我需要通过 Jest 测试测试我对端点的 POST 请求是否正常工作。我的想法是首先获取我的 Services 表的计数(我正在使用 sequelize orm),然后发送一个新的 post 请求并最终获取新计数并比较旧计数 + 1 是否等于新计数,如果为真,那么 POST 请求就可以正常工作。

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}`));
});

});

对我来说,测试看起来不错,但是当我运行它时,我得到:

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

是否缺少某些内容,或者是否有更好的方法来测试 POST 请求?开玩笑?

【问题讨论】:

    标签: node.js express jestjs


    【解决方案1】:

    这是因为你没有调用 jest 回调函数中传递的 done 回调。可以这样做。

    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()
                });
        });
    });
    

    你也可以这样写这段代码,这样可以提高可读性,最大限度地利用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()
        }
    });
    

    默认情况下,Jest 也会在 async/await 情况下解析 Promise。我们也可以在没有回调函数的情况下实现这一点

    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}`)
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2020-03-26
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      相关资源
      最近更新 更多