supertest icon indicating copy to clipboard operation
supertest copied to clipboard

`_body` key instead of `body` in response

Open mrtl-srn opened this issue 2 years ago • 2 comments

Describe the bug

Node.js version: >= 14

OS version: Docker devcontainer with vscode image

Description:

There is a _body key instead of body in response. I don't know if it's an expected behavior due to recent changes but it's a breaking change for all my tests

Actual behavior

// ./app.js

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.json({
    ok: true,
  })
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
});

module.exports = app;
// ./tests/app.test.js

const supertest = require('supertest');
const { expect } = require('chai');
const app = require('../app.js');


const server = supertest.agent(app);

describe("Tests", () => {
  it('should return a JSON object', async () => {
    const response = await server.get('/').expect(200);

    console.log({ response });

    expect(response).to.haveOwnProperty('body');
  });
});

Result

  1) Tests
       should return a JSON object:
     AssertionError: expected { Object (_events, _eventsCount, ...) } to have own property 'body'
      at Context.<anonymous> (tests/app.test.js:14:25)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

In the response object there is no "body", only a "_body"

image

Expected behavior

"body" should be present and test should pass.

Code to reproduce

Here is a replit example : https://replit.com/@MartialSeron/RowdyTenseLocations#tests/app.test.js You just have to run npm test in the shell

Checklist

  • [x] I have searched through GitHub issues for similar issues.
  • [x] I have completely read through the README and documentation.
  • [x] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

mrtl-srn avatar Jan 27 '23 14:01 mrtl-srn

It works up to version 6.1.6, but from version 6.2.0, it doesn't work anymore.

mrtl-srn avatar Jan 30 '23 07:01 mrtl-srn

I am also experiencing this

lamtonylam avatar Jul 23 '24 09:07 lamtonylam