'Cypress: Getting "Parameter supplied to cy.request() contained a circular reference at the path specWindow" when using command inside a command

I have 2 different commands that I intend to use inside each other. The 1st one generates an object and the second one uses it to send as body of a cy.request. They look like this

Cypress.Commands.add("objectGeneration", function(search, method, block){
  let body = {}
  if(search === 'something') {
    body.search = search,
    body.method = method,
    body.block = block
  }
  return body
})


Cypress.Commands.add("sendReq", (search, method, block) {
  cy.request({
    method: 'POST',
    URL: 'URL',
    body: cy.objectGeneration(search, method, block)

  })

})

In my test I call them this way (where each testCase variable comes from a helper object I import into my test case.

it("Name of the test", function(){
  cy.sendReq(testCase.search, testCase.method, testCase.block).then((resp => {..}...
})

When trying to run this, I get the following Cypress error:

The body parameter supplied to cy.request() contained a circular reference at the path specwindow body can only be a string or an object with no circular references.

All of this would work fine when objectGeneration was just a function and not a command. I think my issue might have to do with the return in a command. Any suggestions? Thanks



Solution 1:[1]

Ran into a similar issue trying to send a Decimal value (using decimal.js) as part of the body.

In the end I serialized the object myself and sent it this in the request. In your case I would think that this should also work:

Cypress.Commands.add("sendReq", (search, method, block) {
  const serialized = JSON.stringify(cy.objectGeneration(search, method, block)) 

  cy.request({
    method: 'POST',
    URL: 'URL',
    body: serialized,
    headers: { 'Content-Type': 'application/json' }
  })
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Albert