https://cypress.io logo
How to call a const as a response in a cy.intercep...
# i-need-help
b
I have a cy.intercept that was previously calling a static fixture from my fixture files. I now have to make this fixture dynamic so I created a const. To make this simple, say I did something like this;
Copy code
const getDivisionBody = () => {
    return {
      data: {
        division: {
          utilitiesConfig: {
            electric: false,
          },
          __typename: 'Division',
        },
      },
    };
  };
My fixture had electric set to false but I want to be able to create a function which can toggle the value to false or true. First, I wanted to make sure the intercept could take this variable before I modify it. so I did this:
Copy code
cy.intercept('POST', '/graphql', (req) => {
    if (req.body.operationName === 'getDivision') {
      req.reply(`${getDivisionBody}`);
    }
  });
Now my cypress is able to see the variable but it returns this, including the function part
Copy code
function () {
        return {
            data: {
                division: {
                    utilitiesConfig: {
                        electric: false,
                    },
                    __typename: 'Division',
                },
            },
        };
    }
And if I do
Copy code
cy.intercept('POST', '/graphql', (req) => {
    if (req.body.operationName === 'getDivision') {
      req.reply(getDivisionBody);
    }
  });`
g
getDivisionBody
is a function so you want to do
cy.reply(getDivisionBody())
3 Views