-
Notifications
You must be signed in to change notification settings - Fork 760
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
Cannot read property 'status' of undefined #352
Comments
thanks @jakubknejzlik can you please post a small test snippet showing the problem? |
I'm also experiencing this problem when using multiple callbacks in a single route -
Then The following test fails for me due to the throw above, I use Jest but this could easily be changed to use mocha:
I'm using supertest v1.2.0 and express v4.14.0. |
In my case I was using It might be a good idea to add the waiting logic in a |
This is how I solved it using Promises from Supertest@2.0.0: it('should return 404 Not Found for missing resources', function() {
return req.get('/this/is/not/found')
.expect(404)
.expect(function(res) {
assert.equal(res.body.message, 'Resource Not Found');
});
}); |
Here's a traceback:
|
Similarly,
|
Looks like #348 is a fix |
This happens when I use |
Submitted #348 almost three months back and it should take care of this bug. However, it seems that the repository is not active and nobody is accepting PRs! |
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. Cannot read property 'status' of undefined Hope this helps someone. God bless. |
FWIW. I run into this very error when I run under node 6.9.2 (LTS) but don't see it when I switch to 4.0.0. Is this an ES6 related issue?
|
tracing through my particular issue, |
upgrading all my dependencies, including
|
I have the same issue with
It fails sometimes. About 1 to 10 times. My test looks pretty straight forward:
My deps:
|
@Starefossen It does indeed work when using promises, thanks! |
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:
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. |
This seems random and doesn't make any sense to me. Supertest was working perfectly. I made some very unrelated code changes (eg just a small change to a schema object) and now supertest If I revert the unrelated code change, I get the same error. Wtf? I am unable to run tests at all now because of this. |
@ttbarnes just quick question...does it do the similar when using promises? Eg:
|
@jakubknejzlik by adding the 'return' in the app request, It's not getting to the promise, and just getting a unit test timeout error. eg:
If I remove the 'return' from the initial app request, I get
|
@ttbarnes well that's strange. The latter one should give you timeout not the first one. |
Getting this error, but only on some servers. Code: basicTest.js var server = process.env.SERVER_UNDER_TEST; describe("Basic-test", () => {
});` Unfortunately, the errors occur on two out of three of our internal servers:
It looks like the problem may be due to gzip compression; test server returns: dev server returns: |
Just ran into this as well, very frustrating, and it was already using the promises API, so @Starefossen's fix didn't help. Weirdly, I could request any route other than my site's |
Updating body-parser (https://www.npmjs.com/package/body-parser) to 1.16.0 (in my case) did the trick for me. |
Tried updating various dependencies including body-parser - same issue for me. 👎 |
I have the same issue while run HTTPS local server. The problem disappear with HTTP server. |
@radostnomne see my HTTPS fix above |
I've got the same issue, inconsistently, only for requests on the root path (all other tests pass). Node 6.11.2 |
same issue in gitlab ci
|
Not sure if this is entirely relevant, but we have experienced this issue. The problem in our case was that there were errors generated inside Since errors were thrown before the response was created, the error object, as delivered to the interceptor handler, did not have a "response" so an error was thrown when referencing A scheme that would allow Axios to only catch errors related to the requests and pass others up the chain. |
Same issue here
|
for me only the 4xx response tests started failing with this error due to some npm dependency update. Update: For me this was caused by the response content-type not matching the actual content-type of the error response body (a JSON parse error occurs). This regression occurred due to a change somewhere in the app which set Content-type to |
Just got this error, and my problem is that I forgot the slash in get path:
putting the slash in, 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 |
interesting @01binary , i'm quite certain that I was getting the same error regardless of the slash. |
Like @danday74 said this issue is present when using HTTPS without NODE_TLS_REJECT_UNAUTHORIZED=0. |
#446 should help reveal the underlying error here. |
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. |
I also see this if I make a request against a site that doesn't exist:
It seems any case of an error around the response, either from a system error or some other kind of "Unhandled Error" (e.g., malformed JSON like mentioned above), causes problems when using
|
Could you please give me an example how to add NODE_TLS_REJECT_UNAUTHORIZED=0 as an env var on the test run? |
I'm facing an error very close to this reported error. I'm passing an const app = require('../../src/app');
const request = require('supertest')(app);
const ColorFactory = require("../factories/color");
const UserFactory = require("../factories/user");
...
describe('Test', () => {
it('should be created a new color', async () => {
const user = await UserFactory.build("user");
const registerResponse = await request.post('/auth/register').send(user);
const authorization = `Bearer ${registerResponse.accessToken}`;
const color = await ColorFactory.build("color");
const response = await request
.post('/colors')
.set('Authorization', authorization)
.send(color)
;
expect(response.status).toBe(201);
});
}); And in my middleware is something like that: require('dotenv/config');
const jwt = require('jsonwebtoken');
const jwtSecret = process.env.JWT_SECRET;
module.exports = (req, res, next) => {
const authHeader = req.headers.authorization;
if ( ! authHeader ) {
return res.status(401).send({ errors: { message: "No token provided" } });
}
const parts = authHeader.split(' ');
if ( ! parts.length === 2 ) {
return res.status(401).send({ errors: { message: "Token error" } });
}
const [ scheme, accessToken ] = parts;
if ( ! /^Bearer$/i.test( scheme ) ) {
return res.status(401).send({ errors: { message: "Token malformatted" } });
}
jwt.verify( accessToken, jwtSecret, (error, decoded) => {
if ( error ) {
console.log(error);
return res.status(401).send({ errors: { message: "Token invalid" } });
}
req.userId = decoded.id;
req.userToken = accessToken;
return next();
});
}; The variable |
->Resolve it Unhandled Rejection (TypeError): 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).The text was updated successfully, but these errors were encountered: