-
Notifications
You must be signed in to change notification settings - Fork 758
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
When status code expectation fails, can't figure out which request failed #317
Comments
I have the same problem. I've tried throwing the error from the |
I use the following function to throw an error in var _ = require('lodash');
var checkStatusCode = function(res, expectedStatus) {
if (!expectedStatus) expectedStatus = 200;
if (res.status === expectedStatus) return res;
var description = res.req.method + ' ' + res.req.path;
var data = (res.request && res.request._data) ? res.request._data : null;
var paddedDataStr = (data && _.isPlainObject(data)) ? (' ' + JSON.stringify(data)) : '';
var resStr = (_.isPlainObject(res.body) ? JSON.stringify(res.body) : res.body);
throw new Error(description + paddedDataStr + ' ' + res.statusCode + ', expected ' + expectedStatus + '. Reason: ' + resStr);
}; This gives missing request details and proper stack trace. Of course it's up to personal preferences. |
@sakovias that's very helpful. IMHO this is how supertest should return details on assertion failures. |
Shame this is closed @rimiti. I encountered the exact same issue today. At a minimum, I'd like to include the line in my
What's the current advice for how best to handle this in a Mocha test? |
same @georgecrawford |
@rimiti any chance to reopen this? i am also willing to work on a PR. |
Seeing the same issue as well |
Same for me, having supertest 4.x and supertest-session 4.x and also when using |
Have the same problem with stack traces using e.g. expect(200) method. As this problem is already ~3 years old and, as mentioned by @CatBakun, it cannot be solved by Bluebird-Promises, I have to stop using this package :( |
For me, bluebird wasn't loaded by superagent, so I went inside the module and straight up replaced
3 more lines of nonsense... With |
if it helps, you can stop performing expectations with
This will return the exact file and line of the failed assertion. A bit more verbose... but it makes debugging much faster. |
Would a PR fixing this one be of interest here, @rimiti? I quickly fixed my version locally and the output is now the following:
Not sure it if would fix this one, it is still a bunch of non-sense but at least I can quickly know where the error failed. Maybe someone with more experience could get it better later. |
what if we assume there's interest in a PR for this, then go ahead and open one? How would you solve it? |
Always a good assumption to have, @jpbochi. I will check it out later this week and open the PR. Stay tuned. |
Nice, good stuff, thanks for PR. My solution was very similar to yours: wrapping the assert functions and preserving the stack from a new Error instance. |
should I ping someone to review that PR? |
Hello, I got the same issue today: expected 200 "OK", got 404 "Not Found" Has someone found a solution for this? Thanks in advance. |
This was fixed by @jpbochi PR released in 6.1.0. |
Yep, upgrading to 6.x.x worked for me. I think this issue can be closed. |
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 withsupertest
'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
which gives the following stack trace:
Is this an expected behaviour or this is something I can try to fix?
The text was updated successfully, but these errors were encountered: