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.

Supertest throwing 404, Client says 200

See original GitHub issue

I have a route to get all friends in a database as follows:

router.get('/friends', function(req, res, next){
  Friend.find(function(err, friends){
    if(err){ console.log(err); }
    req.data = friends;
    next();
  })
});

Hitting this route with postman returns a list of all friends in the database successfully, and other queries from a web client behave normally. However, with Supertest when I try to test this route, a 404 is returned.

Here is the test:

var app = require('../server'); 
var should = require('should');
var supertest = require('supertest');
var request = supertest(app);


describe('Friends', function(){

  it('Should get all friends.', function(done){
    request
      .get('/friends')
      .expect(200)
      .end(function (err, res){
        res.status.should.equal(200);
        done();
      })
  });

});

Now, about 20% of the time, this test will execute successfully for no given reason. My question: Why does supertest return a 404, when all other methods to access the route return 200?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:28
  • Comments:20

github_iconTop GitHub Comments

11reactions
lissaucommented, Mar 21, 2021

For anyone stumbling across this from Google:

Make sure you are using .post() to fetch a post endpoint and .get() for a get endpoint.

I know. Pretty silly, but that was my mistake and why I was getting 400’s when postman reported 200

4reactions
sakrlogcommented, Jul 6, 2022

WAW, still facing the same issue. It’s been 5 years since this issue was created

Read more comments on GitHub >

github_iconTop Results From Across the Web

Supertest throwing 404, Client says 200 · Issue #255 - GitHub
If your controller returns void or Promise or undefined it will throw you 404 error. To prevent this if you need to specify...
Read more >
How to fix an endpoint test that returns 404 status code rather ...
but just checking status for get request is not enough because if your db is empty you will still get 200. const response...
Read more >
Api Patch Request Getting 200 In Postman But 404 ... - ADocLib
API patch request getting 200 in postman but 404 with supertest Before starting SuperTest let's create a simple API using node + express....
Read more >
INTEGRATION TESTING CRASH COURSE IN 80 MINUTES ...
In this video we will have an in depth look into the building and Integrated Testing of Our CRUD application using Node JS....
Read more >
supertest - Bountysource
Client returns 200 supertest returns 404 $ 0. Created 1 year ago in visionmedia/supertest with 0 comments. Express App. `require('dotenv').
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