测试ExpressJS端点玩笑(Testing ExpressJS endpoint with Je

2019-09-25 19:37发布

我想利用我的玩笑Express应用程序测试的端点。 我从摩卡迁移尝试玩笑,以提高速度。 然而,我的玩笑测试不收? 我不知所措......

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

Answer 1:

所以,我能想到这个失败的唯一的事情是,你可能会丢失的包babel-preset-env

在任何情况下,还有另外两种方式来使用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就是看中了解决方案,但也有更多的要求之一。 如果你能找到什么问题让我知道:)。

(参考我的回答: http://www.albertgao.xyz/2017/05/24/how-to-test-expressjs-with-jest-and-supertest/ )



Answer 2:

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

如果您的配置设置是否正确的代码应该工作。



文章来源: Testing ExpressJS endpoint with Jest