I am testing very basic REST api with supertest. I want to save the item id received in response body and assign it to a variable. Using this id i want to make further tests like get-item-by-id or update-item-by-id. No official documentation has covered this so a beginner friendly answer would be very helpful.
test i have written
const request = require("supertest")
let id;
describe('Products API', () => {
it('GET /products --> array of products', async () => {
return request('http://localhost:3001')
.get('/api/products')
.expect(200)
.expect('Content-Type', /json/)
.then(response => {
expect(response.body).toEqual(
expect.objectContaining({
success: true,
data: expect.any(Array)
})
)
})
})
})