使用Jest测试ExpressJS端点 [英] Testing ExpressJS endpoint with Jest

查看:384
本文介绍了使用Jest测试ExpressJS端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jest从我的快速应用程序中测试终结点.我正在从Mocha迁移,尝试使用Jest来提高速度.但是,我的Jest测试不会关闭吗?我很茫然...

I'm trying to test an endpoint from my express application using Jest. I'm migrating from Mocha to try out Jest to improve the speed. However, my Jest test does not close? I'm at a loss...

process.env.NODE_ENV = 'test';
const app = require('../../../index');
const request = require('supertest')(app);

it('should serve the apple-app-site-association file /assetlinks.json GET', async () => {
  const response = await request.get('/apple-app-site-association')
  expect(response.statusCode).toBe(200);
});

推荐答案

因此,我对此失败的唯一思考就是您可能缺少软件包babel-preset-env.

So the only thing I could think for this failing is that you might be missing the package babel-preset-env.

无论如何,还有另外两种使用supertest的方法:

In any case there are two other ways to use supertest:

it('should serve the apple-app-site-association file /assetlinks.json GET', () => {
  return request.get('/apple-app-site-association').expect(200)
})

it('should serve the apple-app-site-association file /assetlinks.json GET', () => {
    request.get('/apple-app-site-association').then(() => {
        expect(response.statusCode).toBe(200);
        done()
    })
})

async是很好的解决方案,但它也是有更多要求的解决方案.如果您设法找到问题所在,请告诉我:).

async is the fancy solution, but is also the one that has more requirements. If you manage to find what was the issue let me know :).

(参考我的答案: 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆