Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chai response.body is always empty {}

No matter what my server actually returns, Chai always gives me this exception when I assert response.body:

Uncaught AssertionError: expected {} to deeply equal 'test'

Even though the actual server response is 'test', not {}: enter image description here

Here is my test:

const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();    
chai.use(chaiHttp);

describe('GET /test', () => {
  it('it should give test result', (done) => {
    chai.request(server)
        .get('/test')
        .end((err, res) => {
            console.log(err); // outputs null
            console.log(res); // outputs normal-looking response
            res.body.should.be.eql('test');
            done();
        });
  });
});

Here is my server (test-server.js):

const http = require('http');
const server = http.createServer(function (request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end('test');
});

module.exports = server;

server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");

What am I doing wrong?

like image 495
Sergey Avatar asked Apr 10 '17 10:04

Sergey


1 Answers

Content-Type: application/json

res.body is populated depending on Content-Type header

test-server.js

const http = require('http');
const server = http.createServer(function (request, response) {
    response.writeHead(200, {"Content-Type": "application/json"});

    var b = JSON.stringify({
      name: 'asad',
      class: 'paewe'
    });

    response.end(b);
});

module.exports = server;

server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");

The res.body will contain the Parsed object

test.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);

describe('GET /test', () => {
    it('it should give test result', (done) => {
        chai.request(server)
            .get('/test')
            .end((err, res) => {
                console.log(err); // outputs null
                console.log(res); // outputs normal-looking response
                console.log(res.body) // { name: 'asad', class: 'paewe' }

                var checkObj = {
                    name: 'asad',
                    class: 'paewe'
                }
                res.body.should.be.eql(checkObj); // passes test
                done();
            });
    });
});

--------------------------------------------------------------------------------------------------------

Content-Type: text/plain

If the Content-Type header is text/plain, then the response body won't be parsed as anything, but res.text will contain the data as string

test-server.js

const http = require('http');
const server = http.createServer(function (request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end('test');
});

module.exports = server;

server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");

test.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();    
chai.use(chaiHttp);

describe('GET /test', () => {
  it('it should give test result', (done) => {
    chai.request(server)
        .get('/test')
        .end((err, res) => {
            console.log(err); // outputs null
            console.log(res); // outputs normal-looking response
            console.log(res.body) // {}
            res.text.should.be.eql('test'); // passes test
            done();
        });
  });
});

Some references

  1. https://github.com/visionmedia/superagent/issues/990
  2. https://github.com/visionmedia/supertest/pull/10
  3. https://github.com/visionmedia/supertest/issues/68
like image 193
Parwat Kunwar Avatar answered Sep 29 '22 16:09

Parwat Kunwar