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.

When status code expectation fails, can't figure out which request failed

See original GitHub issue

In my Mocha tests I use before() hooks to send a number of requests to create data required for the actual tests. When one of the requests fails I end up with supertest’s internal stack trace, which doesn’t tell which request failed. The only indication of error is that one of the requests returned with unexpected status code.

Here’s an example

var request = require('supertest');
var url = 'www.example.com';

request(url)
  .get('/')
  .expect(200)
  .end(function(err) {
    if (err) console.log(err.stack);
  });

request(url)
  .get('/test')
  .expect(200)
  .end(function(err) {
    if (err) console.log(err.stack);
  });

which gives the following stack trace:

$ node test.js 
Error: expected 200 "OK", got 404 "Not Found"
    at Test._assertStatus (/my/path/node_modules/supertest/lib/test.js:232:12)
    at Test._assertFunction (/my/path/node_modules/supertest/lib/test.js:247:11)
    at Test.assert (/my/path/node_modules/supertest/lib/test.js:148:18)
    at assert (/my/path/node_modules/supertest/lib/test.js:127:12)
    at /my/path/node_modules/supertest/lib/test.js:124:5
    at Test.Request.callback (/my/path/node_modules/supertest/node_modules/superagent/lib/node/index.js:831:3)
    at Stream.<anonymous> (/my/path/node_modules/supertest/node_modules/superagent/lib/node/index.js:1049:12)
    at Stream.emit (events.js:117:20)
    at Unzip.<anonymous> (/my/path/node_modules/supertest/node_modules/superagent/lib/node/utils.js:108:12)
    at Unzip.emit (events.js:117:20)

Is this an expected behaviour or this is something I can try to fix?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:20
  • Comments:20 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
georgecrawfordcommented, Mar 25, 2019

Shame this is closed @rimiti. I encountered the exact same issue today. At a minimum, I’d like to include the line in my spec file which caused the error! Currently I just get this:

at Test._assertHeader (node_modules/supertest/lib/test.js:247:12)
      at Test._assertFunction (node_modules/supertest/lib/test.js:281:11)
      at Test.assert (node_modules/supertest/lib/test.js:171:18)
      at Server.assert (node_modules/supertest/lib/test.js:131:12)
      at emitCloseNT (net.js:1664:8)
      at _combinedTickCallback (internal/process/next_tick.js:136:11)
      at process._tickDomainCallback (internal/process/next_tick.js:219:9)

What’s the current advice for how best to handle this in a Mocha test?

7reactions
mrstifcommented, May 25, 2020

if it helps, you can stop performing expectations with supertest and use chai or jest instead.

const res = await request(url).get('/')
expect(res.status).to.eq(200)

This will return the exact file and line of the failed assertion. A bit more verbose… but it makes debugging much faster.

Read more comments on GitHub >

github_iconTop Results From Across the Web

417 Expectation Failed - HTTP - MDN Web Docs
The HTTP 417 Expectation Failed client error response code indicates that the expectation given in the request's Expect header could not be met...
Read more >
HTTP POST Returns Error: 417 "Expectation Failed."
Observed this condition when a client is running through a HTTP 1.0 (only) proxy server. The client (asmx proxy without any configuration) is...
Read more >
HTTP/1.1 Status Codes 400 and 417, cannot choose which
The client SHOULD NOT repeat the request without modifications. Regarding the 417 the rfc says. 10.4.18 417 Expectation Failed. The expectation ...
Read more >
When status code expectation fails, can't figure out ... - GitHub
When one of the requests fails I end up with supertest 's internal stack trace, which doesn't tell which request failed. The only...
Read more >
HTTP Status 417: Expectation Failed - MSDN - Microsoft
This means the server is choking on the request. Now all you have to do is find out why? For example we all...
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