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.

expect(!2xx) throws exception

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:22
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
jsstrncommented, 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);
    });
});
4reactions
gjohnsoncommented, Mar 13, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

expect(!2xx) throws exception · Issue #556 · ladjs/supertest
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....
Read more >
TestRestTemplate throws exception for 4xx status codes
The problem I'm encountering is that because my REST call expects a complex JSON, for blocked calls the tests fail because TestRestTemplate is ......
Read more >
Response validation - Ktor
Use the expectSuccess property to throw exceptions for non-2xx responses. Add stricter validation of 2xx responses. Customize validation of non-2xx ...
Read more >
Java Examples & Tutorials of StatusResultMatchers.is (org ...
public void testMatcher() throws Exception { this.mockMvc.perform(get("/badRequest")).andExpect(status().is(equalTo(400)));
Read more >
Spring Web Client Exception Handling | by David - Medium
Usually when handling HTTP exceptions, you focus on two different types; one is when the request is successful but the resource returns a...
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