Skip to content
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

expect(!2xx) throws exception #556

Closed
midrissi opened this issue Mar 9, 2019 · 12 comments
Closed

expect(!2xx) throws exception #556

midrissi opened this issue Mar 9, 2019 · 12 comments
Assignees

Comments

@midrissi
Copy link

midrissi commented Mar 9, 2019

I have migrated to the latest version of supertest (4.0.0), but all my tests that expect a status code different to 2xx fails.

Example:

const request = require('supertest');
const express = require('express');

const app = express();

app.get('/user', function (req, res) {
  res.status(404).end();
});

(async () => {
  await request(app)
    .get('/user')
    .expect(404)
    .send({ hello: 'hi' })
})();

Will throw an Error: Not Found exception.

@midrissi midrissi changed the title expect(!2xx) throws exeption expect(!2xx) throws exception Mar 9, 2019
@midrissi
Copy link
Author

A simple test case:

it('should assert using promises', function (done) {
  const app = express();

  app.get('/', function (req, res) {
    res.status(400).end();
  });

  request(app)
    .get('/')
    .expect(400)
    .then(done)
    .catch(done);
});

@Jackie-Tan
Copy link

Jackie-Tan commented Mar 10, 2019

Updated: revert to v3.4.2. Everything back to normal.

I have similar issue too.
My code:

const request = require('supertest');
const express = require('express');

const app = express();

app.get('/user', function (req, res) {
  res.status(400).send();
});

test('Should pass this test with status 400', async () => {
	await request(app).get('/user').expect(400)
})

Expect the test to pass, but instead:

 FAIL  tests/testing.test.js
  ● Should pass this test with status 400

    Bad Request

      at Test.Object.<anonymous>.Request.callback (node_modules/superagent/lib/node/index.js:804:15)
      at IncomingMessage.parser (node_modules/superagent/lib/node/index.js:1036:18)

@kevinbosman
Copy link

This is the same issue as #534, which was caused by upgrading superagent to v4.1.0. This previous issue was resolved at the time by downgrading superagent to v3.8.3.

superagent has again been upgraded without a proper fix in place.

@ahce
Copy link

ahce commented Mar 10, 2019

async and promises are broken
Only works this:

request(app)
    .get('/')
    .expect(400)
    .end(done);

@bonesoul
Copy link

same here.

@rimiti
Copy link
Contributor

rimiti commented Mar 11, 2019

@gjohnson Do you have some time for fixing that? 🙏

@posquit0
Copy link

same here

@fega
Copy link

fega commented Mar 11, 2019

Same here, I'm not sure if this change was intented, but a !2XX response shouldn't trigger a failure

@jbinto
Copy link

jbinto commented Mar 11, 2019

Worth following upstream: ladjs/superagent#1466

@gjohnson
Copy link
Contributor

@rimiti looking at this right now - these reports are much clearer @midrissi.

@gjohnson gjohnson self-assigned this Mar 13, 2019
@gjohnson gjohnson added needs-investigation and removed need-help Need help labels Mar 13, 2019
@jsstrn
Copy link

jsstrn commented Mar 14, 2019

Thanks for reporting the issue. While we are waiting for a fix, we can use this technique to resolve it:

it("should return status code 400", () => {
  return request(app)
    .get("/")
    .catch(res => {
      expect(res.status).toBe(400);
    });
});

Or, with async/await:

it("should return status code 400", async () => {
  await request(app)
    .get("/")
    .catch(res => {
      expect(res.status).toBe(400);
    });
});

@rimiti
Copy link
Contributor

rimiti commented Mar 15, 2019

#558 merged, I close this issue.

Thank you 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests