supertest: Cannot read property 'status' of undefined
When sending test request with expected status code and getting response with invalid content type (eg. header says application/json, but server sends xml/html), the Test.prototype._assertStatus
fails while getting status from res (which is undefined).
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 51
- Comments: 40 (2 by maintainers)
I got this error when testing over a secure connection. I fixed it by adding NODE_TLS_REJECT_UNAUTHORIZED=0 as an env var on the test run.
For those wondering why this happens, it can happen because ‘127.0.0.1’ is not listed as a valid IP in your certificate. The underlying error is … Hostname/IP doesn’t match certificate’s altnames: "IP: 127.0.0.1 is not in the cert’s list: " However, you will not see this error message - Just the spurious and much disliked …
Cannot read property ‘status’ of undefined
Hope this helps someone. God bless.
Just got this error, and my problem is that I forgot the slash in get path:
supertest(server).get('favicon.ico')
gets me…putting the slash in,
supertest(server).get('/favicon.ico')
…the test works as expected.I traced the issue down to logic that applies assertions. It’s supposed to early-exit with an error if there was an error, but something causes it to run the assertions anyway and it fails because response is undefined when there’s an error:
Maybe it fails because the un-intended effect of forgetting the slash means that it will try to connect to “http://favicon.ico” which isn’t a server?
Either way it needs some logic hardened in
Test.assert
(or whatever runs through the array of assertions and applies them) and a friendly error message saying itCouldn't connect to http://favicon.ico:80 to run the tests
instead of throwing some IP number out - there is no way to know what that means without reading the whole library.I have the same issue with
node 7.2.1
:It fails sometimes. About 1 to 10 times. My test looks pretty straight forward:
My deps:
https://github.com/visionmedia/supertest/pull/446 should help reveal the underlying error here.
Tried updating various dependencies including body-parser - same issue for me. 👎
This is how I solved it using Promises from Supertest@2.0.0:
Like @danday74 said this issue is present when using HTTPS without NODE_TLS_REJECT_UNAUTHORIZED=0.
Will there be a fix for this soon? Does anyone have a workaround for this?
This only seems to happen with POST and PUT requests, for me at least.
I’ve tried upgrading relevant packages (express, bluebird, http-status, mocha, node, supertest), using promises as suggested above, downgrading to node v5/v4, getting latest mac OS and xcode - still getting the same supertest error:
Unhandled rejection TypeError: Cannot read property 'status' of undefined
Here is one example that fails - this used to work, I don’t understand why this is no longer working.
This is completely blocking me atm. Maybe superagent is the way to go.
Here’s a traceback:
I’m getting this when the response body has a malformed JSON. It’s good that the test fails, but it’s for the wrong reasons and also with a cryptic message. Would it be possible to not try to parse the body unless I’m reading from it? At least don’t give a catastrophic failure just because the response body can’t be parsed.
Looks like #348 is a fix
Updating body-parser (https://www.npmjs.com/package/body-parser) to 1.16.0 (in my case) did the trick for me.