0

I'm trying to assert if response body is empty, but got error:

var api = supertest(TEST_URL);
...
api.get('..')
   .expect('Content-Type', /json/)
   .end(function (err, res) {
      if (err) {
        return done(err);
      }

      res.should.have.status(200);

      // Uncaught AssertionError: expected {} to have a property 'length'
      // res.body.should.empty;

      // Workaround should be used
      res.text.should.eql('{}');

What's wrong? How can I fix this issue?

hellboy
  • 1,567
  • 6
  • 21
  • 54

1 Answers1

1

.empty assertion in should.js check for string, arguments, arrays just length property. So if it throw assertion about missing length - your body does not looked parsed to json (you need to make sure you return right Content-Type like application/json).

For objects yes .empty will check for missing any enumerable properties.

$ node
> var should = require('should')
undefined
> var res = { body: {} };
undefined
> res.body.should.be.empty
{ obj: {},
  params: { operator: 'to be empty' },
  negate: false }
> 
den bardadym
  • 2,747
  • 3
  • 25
  • 27
  • res.type = "application/json", res.text = "{}", res.body = Object, res.should.be.a.json passed – hellboy Sep 08 '14 at 08:23
  • @hellboy i added example that in REPL it works. So i assume something wrong with your `body` (i used latest should.js 4.0.4). – den bardadym Sep 08 '14 at 09:51