Response.body is empty in this test that uses supertest and node.js

Viewed 52

Using supertest to test a node.js api for unit and integration testing but the test for integration keep failing; I don't know why I keep receiving an empty object in the reponse.body while I am expecting a result of 30 from the AddNum object. the error comes from the file app.test.js line 19

This is my code for app.test.js


const supertest = require('supertest');
const server = require('../../app');


describe("Calculate", () => {
    it('POST /calculate: action: sum', async () => {
       const  AddNum = {
            action: 'sum',
            num1: 20,
            num2: 10
        }

        
        const response = await supertest(server).post("/calculate").send(AddNum)
        console.log({response})
        expect(response.status).toBe(200)

        const expectResult = {result:30}
        expect(response.body).toBe(expectResult)
    })
})

This is my error from running the test in the command line.

      at Object.log (test/integration/app.test.js:15:17)

  ● Calculate › POST /calculate: action: sum

    expect(received).toBe(expected) // Object.is equality

    - Expected  - 3
    + Received  + 1

    - Object {
    -   "result": 30,
    - }
    + Object {}

      17 |
      18 |         const expectResult = {result:30}
    > 19 |         expect(response.body).toBe(expectResult)
         |                               ^
      20 |     })
      21 | })

      at Object.toBe (test/integration/app.test.js:19:31)

A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks. Active timers can also cause this, ensure that 
.unref() was called on them.
Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 4 skipped, 1 passed, 6 total
Snapshots:   0 total
Time:        2.556 s
Ran all test suites.

0 Answers
Related