Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

102
Views
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.

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

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
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs