Supertest response body always empty?

Viewed 16

I'm using jest and supertest to test some api endpoints, and struggling to view the response body.

The response body is always an empty object for me, and I'm not really sure why.

Here is the example of my test:

 test('POST /api/transactions should fetch max records on a page with a cursor', done => {
const cursor = generateCursor('xyz');
request(server).post('/api/transactions/search').send({
  walletAddr: process.env.TESTING_WALLET_ADDRESS,
  ensName: 'enstrackertest.eth',
  chainId: 4,
  cursor,
  pageSize: 2
})
.set('Accept', 'application/json')
.then( res => {
  console.log(res.body);
  expect(res.statusCode).toBe(200);
  expect(res.ok).toBeTruthy();
  done();
});

My server call:

    const app = express();

app.use(cookieParser());
app.use(helmet());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cors({ origin: process.env.APP_HOSTNAME, credentials:true }));
app.use(corsMiddleware);
app.use(morgan('combined'));

export const server = app;

The endpoint I'm calling returns:

 return res.status(200).json({records: txRecords, cursor: paginationCursor});

I'm expecting that res.body would show these two attributes, records and cursor. I want to view the returned body to accurately test the endpoint.

0 Answers
Related