chai expect(res.body) fails consistently

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

Response status passes and fails under my control. But I can’t see to get the expect(res.body) test to pass. Route displays exactly as programmed when viewed in the browser. but mocha/chai test for testing what is rendered in the browser always fails. What am I doing wrong??
health-check route code:

import express from 'express';
const router = express.Router();   // eslint-disable-line new cap
// GET /health-check - checking for basic server reaction
router.get('/health-check', (req, res) =>
    res.status(200).send('CONGRATS')
);
export default router;

mocha test:

it('should return 200 and res.body of OK', (done) => {
        request(app)
            .get('/api/health-check')
            .expect(200)
            .end(function(err, res) {
                if(err)
                    return done(err);
                expect(res.body).to.equal('CONGRATS');
                return done();
            });

    });
solved 0
1 Answer 27 views 0

Answer ( 1 )

    0
    January 11, 2017 at 6:00 pm

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    Ok, I figured it out. I had to console log the response and then dive into that until I got it to respond correctly. In my sameple above, it was returning an empty set or null. My code that tends to answer my issue is:

    .end(function(err, res) {
          if(err)
             return done(err);
          console.log(res.text);
          expect(res.text).to.equal('CONGRATS');
          return done();
    

    my original idea was that I had to target the “.body” of the response to test against it. Doesn’t appear to be the case here.

    Best answer

Leave an answer

Browse

What is the capital of Egypt ? ( Cairo )

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>