question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.expect(201) passes when res.status == 400

See original GitHub issue

Sorry to create another ticket out of what may be ignorance but…

describe('POST /', function(){
  it('should pass with good img_uri', function(done){
    request(app)
        .post('/')
        .send({
            img_uri: '/foo.png'
        })
        .expect(201)
        .expect('Content-Type', /json/)
        .expect( { ok: 1 } )
        .end(function(err, res){
            console.log( 'BODY: ' );
            console.log( res.body );
            console.log( res.status );
            console.log( 'res.status == 201: '+ res.status == 201 );
            done()
        })
    })
});

The output (in TAP format):

BODY: 
{ ok: 0, msg: 'Missing parameters: img_uri' }
400
false
ok 1 POST / should pass with good img_uri

This seems to show that either the chained .expect() calls are behaving differently than in the examples, or that I failed to understand how they should work.

TIA Lee

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
DjebbZcommented, Oct 2, 2012

I happen to have the same problem, and found how to solve it simply.

Do as @mfrobben says, expect(SOME_CODE, done) when you don’t need complete access to the error and the response.

When you need complete access to do custom stuff in the end callback, test the error as follows :

.end(function(err, res) {
  if (err) {
    done(err);
  } else {
    // do your stuff here
  }
});

I first tried without an else statement, but I got this error : Error: done() called multiple times.

0reactions
eeblecommented, Jul 1, 2013

I was getting this error but it turned out it was because I wasn’t separating my redirect/render code paths properly. I forgot to put a return after the redirect so it ended up redirecting then rendering on the same response object.

Read more comments on GitHub >

github_iconTop Results From Across the Web

expect(201) passes when res.status == 400 · Issue #11 - GitHub
Sorry to create another ticket out of what may be ignorance but... ... This seems to show that either the chained .expect() calls...
Read more >
Why is NodeJS crashing when API returns 400 HTTP status ...
json(response); //if all is OK it will return 201, but if not 400 };. So I've tried catching the status code in addReservation...
Read more >
A Complete Guide and List of HTTP Status Codes - Kinsta
These are error codes specifying that there's a fault with your browser and/or request. 400: “Bad Request.” The server can't return a response...
Read more >
A testing guide for Express with request and response ...
To test an Express handler, it's useful to know how to successfully mock/stub the request and response objects. The following examp.
Read more >
What are the Different types of Asserts in Postman? - Tools QA
Note: 201 is created and 202 is Accepted. Press send and see the response which will be pass if the status code is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found