протестировать тело ответа с помощью Jest и супертеста

У меня есть простой экспресс-сервер http, который возвращает «Hello worl» при выдаче get to /

И у меня есть следующий тест:

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 в виде текста, чтобы проверить его?


person opensas    schedule 10.03.2019    source источник


Ответы (1)


похоже, что тогда возвращается только текст (без json), который вы используете res.text, например:

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)
});
person opensas    schedule 10.03.2019