'ExpressJS res.send() not sending the data

I'm not sure why the GET request can't send me back the data
Basically, my GET request looks like:

router.get('/', (req, res) => {
res.set('Content-Type', 'text/html')
res.status(200).send(Buffer.from('<p>some html</p>'))
});

Then I use supertest to test this route.

  test('test', async () => {
    const res = await request(app)
      .get('/')

    expect(res.statusCode).toBe(200);
    expect(res.body.toString()).toBe("<p>some html</p>");
  });

Then I console.log(res.body), it returns me the empty{}
I'm not sure why the route return give me the empty obj.



Solution 1:[1]

The response data should be get from res.text not res.body.

app.js:

const app = require('express')();

app.get('/', (req, res) => {
  res.set('Content-Type', 'text/html');
  res.status(200).send(Buffer.from('<p>some html</p>'));
});

module.exports = app;

app.test.js:

const app = require('./app');
const request = require('supertest');

describe('71328567', () => {
  test('should pass', async () => {
    const res = await request(app).get('/');
    expect(res.type).toBe('text/html');
    expect(res.text).toBe('<p>some html</p>');
  });
});

Test result:

 PASS  stackoverflow/71328567/app.test.js
  71328567
    ? should pass (26 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.657 s

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 slideshowp2