supertest: Supertest throwing 404, Client says 200
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?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 30
- Comments: 25
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
WAW, still facing the same issue. It’s been 5 years since this issue was created
I had similar issue too using supertest. The highlight here is to make sure you are using
.post()
and.get()
correctly. For instance,This is the
app.ts
file where I initiate the routes.This file
signup.ts
for user signing up.In your test file, you have to make sure if testing this
signup.ts
file, you have to use.post()
rather than.get()
because that is the route initialized in thesignup.ts
. Something like this:In case, where it works in Postman because it assorts to the request type automatically.
Hope this helps.
Hi, I’m having the same issue! Some of my routes throw 404 in my tests but when I literally copy/paste it to postman, it works
Long shot here, but in my case, the error was caused by
koa-cors
. That middleware causes Koa to return a 404 if the origins do not match. (I switched to the officialkcors
gem and all is well now.) Anywho, it might be worth disabling middleware and testing again to see if you can rule out middleware behavior causing a 404 in your test environment.+1 anyone find a solution to this? I have a restify api that with the same behavior, test = 404, but postman = 200. It looks like the test a are firing before the app is ready, but in restify, as far as i can tell, you cant emit an event for mochas before function.
@younes-io I have an API route (prefix with “api”) that is present in the app.js file and all other routes are used inside this API route. All these routes return 404 when used with supertest.
App.js
app.use(apiRoutes);Routes.js
now when testing route GET
api/club/
, I get 404 but get 200 in postmanLate to the game, but make sure your
app.js
file exposes the routes.