0

I am learning Cypress and automating API tests. I have a JSON file in the fixtures folder and I want to pass this JSON file as a request body.

{
  "name": "Harry",
  "email": "hello@cypress.io",
  "mobile": "1234"
} 

so if I want to change the mobile value to something else then I can do like

cy.fixture('request.json').then((requestBody) => {
      requestBody.mobile = '5678'
      cy.request({
        url: requestUrl,
        method: 'POST',
        body: requestBody
    })
})

If I want to delete the mobile value and just want to pass name and email from request.json then can I do this from the same file or I have to create a separate file. Is it possible in cypress?

 requestBody.mobile.delete() //something like this
Aleksey L.
  • 35,047
  • 10
  • 74
  • 84
Harry
  • 4,705
  • 17
  • 73
  • 101

1 Answers1

1

You could use object destructuring to omit the mobile property:

cy.fixture('request.json').then(({ mobile, ...requestBody }) =>
  cy.request({
    url: requestUrl,
    method: 'POST',
    body: requestBody
  })
)
Aleksey L.
  • 35,047
  • 10
  • 74
  • 84
  • Thanks, @Alekse I am using ```delete mobile ``` this is also working. Not sure which one is better – Harry Oct 12 '20 at 21:01
  • In this example doesn't really matter I guess. The difference is that `delete` mutates the object – Aleksey L. Oct 13 '20 at 04:13