ladjs / supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
MIT License
13.79k stars 757 forks source link

got 415 "Unsupported Media Type" #696

Closed danish45007 closed 3 years ago

danish45007 commented 3 years ago

The test operation is mentioned below where I am specifying content-type as application/JSON but still receives Unsupported Media Type I have two different mongo-DB one for prod(which is on atlas) and a local mongo instance for testing plus I am using pusher lib which has different keys set based on the environment

Error

ReferenceError: You are trying to import a file after the Jest environment has been torn down.

  at Object.toObject (node_modules/mongoose/lib/utils.js:290:27)
  at model.Query.Object.<anonymous>.Query.find (node_modules/mongoose/lib/query.js:1989:22)
  at model.Query.Query [as constructor] (node_modules/mongoose/lib/query.js:97:10)
  at new model.Query (node_modules/mongoose/lib/model.js:4747:11)
  at Function.find (node_modules/mongoose/lib/model.js:2037:14)

C:\Users\DANISH\Desktop\My-Space\Nuts-app A full whatsapp clone\server\test\Messages.test.js:22 throw err; ^

Error: expected 201 "Created", got 415 "Unsupported Media Type" at Test.Object..Test._assertStatus (C:\Users\DANISH\Desktop\My-Space\Nuts-app A full whatsapp clone\server\node_modules\supertest\lib\test.js:270:12) at Test.Object..Test._assertFunction (C:\Users\DANISH\Desktop\My-Space\Nuts-app A full whatsapp clone\server\node_modules\supertest\lib\test.js:285:11) at Test.Object..Test.assert (C:\Users\DANISH\Desktop\My-Space\Nuts-app A full whatsapp clone\server\node_modules\supertest\lib\test.js:175:21) at Server.localAssert (C:\Users\DANISH\Desktop\My-Space\Nuts-app A full whatsapp clone\server\node_modules\supertest\lib\test.js:133:12) at Object.onceWrapper (events.js:416:28) at Server.emit (events.js:310:20) at emitCloseNT (net.js:1657:8) at processTicksAndRejections (internal/process/task_queues.js:83:21) ::ffff:127.0.0.1 - GET / HTTP/1.1 200 29 - 4.636 ms ::ffff:127.0.0.1 - GET /v1/api/messages/sync HTTP/1.1 500 2732 - 9.292 ms `

Test-query

test('should return message create response', () => { request(app) .post('/v1/api/messages/new') .send({ message: 'I am unable to get message', name: 'danish', timestamp: '1234', received: true, }) .set('Content-Type', 'application/json') .set('Accept', 'application/json') .expect(201) .end((err, res) => { if (err) { throw err; } console.log(res.body); }); }); `

danish45007 commented 3 years ago

the issue is resolved now we can create a different file for test setup that includes all the test configuration for the database/caching engine etc with all the methods to the setup the config by wrapping inside the wrappers jest provide and export them in that ever test its needed for reference check this https://github.com/danish45007/nutsapp-/blob/master/server/test-setup.js