I would love a recommendation on scaling my Cypress test harness. I have lots of fixtures and many of my fixtures are similar. So, for example, I might have a spec that has five tests testing five fixtures that are all identical except for one line. This is not scalable. They should be taking one fixture and using a parameter to dynamically update that one line.
I started trying to break out one fixture with a command that is similar to this:
Cypress.Commands.add('setDivisionsIds', (Array) => {
cy.intercept(
'GET',
`**/${Cypress.env('API_BASE_URL')}/v2/divisions/*`,
(req) => {
req.reply({ division
// ids: Array,
where the last line of that code is the first line of a 400 line json response that is deeply nested.
When I call `cy.setDivisionsIds([‘1’, ‘2’]);
it does mutate the fixture and now I can write a test with a single ID or a test with more than 1 IDs and it will return that mutated intercept instead of calling two fixtures.
However, moving the 400 line fixture from my fixture folder to commands.ts is clogging up my commands.ts and isn’t scalable. I want to be able to find cypress commands easily in this file and keep fixtures separate. Some of these fixtures are also used by jest, which is why they’re in a /fixture file in my /src and not in /cypress.
I was thinking of creating something like a handler and saving it as a file called DivisionFixture.ts in my fixtures folder
However, I’m not sure how to create a function that returns the json object with my mutated parameter. ie. if I try to create a function in DivisionFixture.ts and export it like this
export function setDivision(Array) {
return [
external_ids: Array,
... rest of json
All my json gets underlined with <key name> not found. Do I need to stringify this value and how would i do that in a function? Or do I need to create a whole class and export that? That could easily get very hairy with the size of my json files and the nesting (up to 10x).
Does anyone have a recommendation? Again steps are:
- Create a Cypress.command that sets a value from an intercept
- Reply in the intercept with the json body that is mutated with the value
- Do this with the help of an external file to keep the test harness scalable