Monday, 10 April 2017

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'

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?



via Sergey

No comments:

Post a Comment