Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.expect(status[, fn]) expect undefined response body !?! #39

Closed
tlindig opened this issue Nov 29, 2012 · 5 comments
Closed

.expect(status[, fn]) expect undefined response body !?! #39

tlindig opened this issue Nov 29, 2012 · 5 comments

Comments

@tlindig
Copy link

tlindig commented Nov 29, 2012

Hallo,

I tried to use this module to test my express app. But I got a strange error.

You may can reproduce it, if you try the following code with mocha. it based on your example.js:

var request = require('supertest')
  , express = require('express');

var app = express();

app.get('/user', function(req, res){
    res.format({
        json : function(req, res, next) {
            res.send( { name: 'tobi' } );
        }
    });
});

describe('GET /users', function(){
    it('respond with json', function(done){
        request(app)
            .get('/user')
            .set('Accept', 'application/json')
            .expect('Content-Length', '20')
            .expect(200)
            .expect('Content-Type', /json/)
            .end(function(err, res){
                if (err) { return done(err); }
                done();
            });
    });
});

If I run it with mocha, I got the following error message:

  GET /users
    1) respond with json


  × 1 of 1 test failed:

  1) GET /users respond with json:
     Error: expected undefined response body, got '{\n  "name": "tobi"\n}'

Why it expect an undefined body?
I guess that .expect(status[, fn]) delegates the call to .expect(status, body[, fn]) and that check the body to be undefined.

@cjroebuck
Copy link

Yep. Just came across this too. I wrote these tests to demonstrate the issue - first one is taken from the readme:

it('should assert status only 1', function(done){ 
  var app = express();

  app.get('/user', function(req, res){
    res.send(201, { name: 'tobi' });
  });

  request(app)
    .get('/user')
    .expect('Content-Type', /json/)
    .expect('Content-Length', '20')
    .expect(201)
    .end(function(err, res){
      if (err) throw err;
    });
})

it('should assert the status only 2', function(done){
  var app = express();

  app.set('json spaces', 0);

  app.get('/', function(req, res){
    res.send({ foo: 'bar' });
  });

  request(app)
  .get('/')
  .expect(200)
  .end(function(err, res){
    err.message.should.equal('expected { foo: \'baz\' } response body, got { foo: \'bar\' }');

    request(app)
    .get('/')
    .expect({ foo: 'bar' })
    .end(done);
  });
})


 1) request(app) .expect(body[, fn]) should assert status only 1:
  Error: Error: expected undefined response body, got '{\n  "name": "tobi"\n}' (undefined:undefined)
   at process.uncaught (/Users/cjroebuck/Dev/supertest/node_modules/mocha/lib/runner.js:492:19)
   at process.EventEmitter.emit (events.js:126:20)

2) request(app) .expect(body[, fn]) should assert the status only 2:
  Error: AssertionError: expected 'expected undefined response body, got \'{"foo":"bar"}\'' to equal 'expected { foo:    \'baz\' } response body, got { foo: \'bar\' }' (undefined:undefined)
   at process.uncaught (/Users/cjroebuck/Dev/supertest/node_modules/mocha/lib/runner.js:492:19)
   at process.EventEmitter.emit (events.js:126:20)

It must be the change to support multiple body assertions that has caused this bug. The workaround for now is to either don't use expect and do your assertion in the end function callback. So instead of .expect(200) it would be .end(function(err,res) { res.status.should.equal(200) }, or if you do use expect.. you need to make sure you specify a body aswell as just a status..

@tj
Copy link
Contributor

tj commented Nov 30, 2012

a pull-requested test would be more helpful, then I can take a look at fixing it

@fengmk2
Copy link
Contributor

fengmk2 commented Dec 3, 2012

I reappear this bug and also fixed it on #41

@ragulka
Copy link

ragulka commented Dec 7, 2012

When will this be put to npm? Almost all my tests are failing at the moment

@tj tj closed this as completed Dec 7, 2012
@tj
Copy link
Contributor

tj commented Dec 7, 2012

0.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants