我有一个简单的快速http服务器,该服务器在发布get到/
时返回“ Hello worl”我有以下测试:
import request from 'supertest';
import app from '../app/app';
test('test http server', async () => {
const res = await request(app).get('/')
expect(res.body).toEqual('Hello world');
});
测试失败,如下所示:
● test http server
expect(received).toEqual(expected)
Expected: "Hello world"
Received: {}
7 | const res = await request(app).get('/')
8 |
> 9 | expect(res.body).toEqual('Hello world');
如何获取response.body作为文本进行检查?
答案 0 :(得分:0)
似乎只返回您使用的res.text
文本(没有json),就像这样:
test('test http server', async () => {
const res: request.Response = await request(app).get('/')
expect(res.type).toEqual('text/html');
expect(res.text).toEqual('Hello world');
});
另一方面,当测试返回json的终结点时,我可以这样做:
test('test valid_cuit with a valid case', async () => {
const cuit: string = '20-24963205-9'
const res: request.Response = await request(app).get(`/api/valid_cuit/${ cuit }`)
expect(res.type).toEqual('application/json')
expect(res.body.cuit).toEqual(cuit)
expect(res.body.isValid).toBe(true)
});