使用Jest测试ExpressJS端点
我正在尝试使用Jest测试express应用程序中的端点。我从摩卡迁移到这里,尝试开玩笑来提高速度。然而,我的玩笑测试没有结束?我不知所措使用Jest测试ExpressJS端点,express,testing,tdd,jestjs,Express,Testing,Tdd,Jestjs,我正在尝试使用Jest测试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.j
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
在任何情况下,使用supertest还有两种其他方法:
it('should serve the apple-app-site-association file /assetlinks.json GET', () => {
return request.get('/apple-app-site-association').expect(200)
})
或
async
是一种奇特的解决方案,但也是一种要求更高的解决方案。如果您找到了问题所在,请告诉我:)
(参考我的答案:)
如果您的配置设置正确,则此代码应该可以工作 不幸的是没有。它仍然没有退出。我想问题是与关闭与mongoose的连接有关的?为什么不模拟mongoose调用?可以使用jest.mock('mongoose',()=>({themethodyouuse:jest.fn()}))const mongoose=require('mongoose'))
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()
})
})
it("should serve the apple-app-site-association file /assetlinks.json GET", async () => {
await request
.get("/apple-app-site-association")
.send()
.expect(200);
});