Testing a Node Js Post Route with supertest and jest

I am trying to test an api post endpoint with supertest and jest but the post payload sent by the test is empty. If i use postman to send the post request the endpoint works correctly. I have tried changing around everything but cant get it to work. Here is my test code:


const request = require('supertest');    // import Supertest
const express = require("express");
const users = require("../routes/users");
const app = express();

app.use(express.urlencoded({ extended: false }));
app.use("/", users);

// use npm run test to run tests


it('should return the solved puzzle', async () => {
  const res = await request(app)
    .post('/solve')
    .set("Content-Type", 'application/json')
    .send({"puzzle": ".1.5..2.84..63.12.7.2..5.....9..1....8.2.3674.3.7.2..9.47...8..1..16....926914.37."})
  expect(res.statusCode).toBe(200);
  // Optionally, check that an error message is returned in the body
  expect(res.body).toBe("135762984946381257728459613694517832812936745357824196473298561581673429269145378");
});

here are the console log results for the req.body in the endpoint code in the command prompt:

console.log
{}

and the console log results for the req.body in the endpoint code in the command prompt when the route is checked with postman:

{
puzzle: ‘1.5..2.84..63.12.7.2..5…9..1…8.2.3674.3.7.2..9.47…8..1..16…926914.37.’
}

If anyone has any idea what I am doing wrong here I would appreciate the help

Craig

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.