Nothing received when I test POST api

Viewed 34

I'm trying to test if I can POST something in my api. However, it's returning undefined. I've tested it using Hoppscotch, the object is there, it's just the test isn't passing

test result

1

expect(received).toEqual(expected) // deep equality

    Expected: {"height": 5.1, "id": 5, "lightsaber": null, "name": "chewie"}
    Received: undefined

      49 |             //     console.log(res.body)
      50 |             // })
    > 51 |             expect(api.body).toEqual(testData)
         |                              ^
      52 |
      53 |     })
describe('api server', () => {
    let api;


    test ('responds to post /starwars with status 201', () =>{
        const testData = {
            id: 5,
            name: 'chewie',
            height: 5.1,
            lightsaber: null
        }
        request(api)
            .post('/starwars')
            .send(testData)
            .set('Accept', 'application/json')
            .expect(201)
            expect(api.body).toEqual(testData)

    })

})
1 Answers

You have to save the api request into a variable than use it to compare if it equals with some value like this:

test ('responds to post /starwars with status 201', async () =>{
        const testData = {
            id: 5,
            name: 'chewie',
            height: 5.1,
            lightsaber: null
        }
        const response = await request(api)
            .post('/starwars')
            .send(testData)
            .set('Accept', 'application/json')
            .expect(201)

            expect(response.body).toEqual(testData)

    })
Related