supertest icon indicating copy to clipboard operation
supertest copied to clipboard

Type Error: res.status is not a function supertest

Open NBNARADHYA opened this issue 6 years ago • 5 comments

`TypeError: res.status is not a function

  49 |     })
  50 |     .catch(error => {
> 51 |       return res.status(400).json({
     |                  ^
  52 |         success: false,
  53 |         error,
  54 |         results: null

  at status (Routes/Controllers/auth/index.js:51:18)`

router.post('/signup', (req, res) => { let validate = ajv.compile(signupSchema); let valid = validate(req.body); if (!valid) { return res.status(400).json({ success: false, error: sumErrors(validate.errors), results: null }); } auth .signup(req.body) .then(results => { console.log(res.json); return res.status(200).json({ success: true, error: null, results }); }) .catch(error => { return res.status(400).json({ success: false, error, results: null }); }); });

Link to the project online-judge

When testing, with supertest this problem seems to exist.

But the same code works perfectly fine when I start the server manually using npm start and when requests are sent using postman.

NBNARADHYA avatar Sep 07 '19 04:09 NBNARADHYA

Maybe you have to mock the response function.

One example:

beforeAll(async () => {
    const res = jest.fn(() => {
    	status: jest.fn(() => {
    		send: jest.fn()
    	})
    })
})

lukaswilkeer avatar Sep 08 '19 11:09 lukaswilkeer

Maybe you have to mock the response function.

One example:

beforeAll(async () => {
    const res = jest.fn(() => {
    	status: jest.fn(() => {
    		send: jest.fn()
    	})
    })
})

But is'nt this supposed to work even without mocking.. I have the link for the project if u want online-judge

NBNARADHYA avatar Sep 10 '19 13:09 NBNARADHYA

@NBNARADHYA are you still experiencing this issue? How exactly are you using supertest?

jonathansamines avatar Oct 05 '19 04:10 jonathansamines

It happens when you try to test the express.Router() instance. Using express() instead of express.Router() solved my problem.

Askerov-A avatar Mar 22 '20 22:03 Askerov-A

Can you explain why express() works differently from express.Router() when using supertest?

beckaberhanu avatar Jul 14 '21 02:07 beckaberhanu