Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
How to Receive cypress interception fixture twice but in different format?

I'm a beginner with Cypress and I am stuggeling with the following:

I am calling the following from my test file:

cy.SetupClassificationsStubFixture(1);

This refers to the following command:

Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {

cy.fixture('ClassificationsStub.json').then(classificationsStub => {
    const slicedClassifications = classificationsStub.slice(0, amount)
    cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')


cy.fixture('ClassificationsStub.json').then(classificationsStub => {
    const slicedClassifications = classificationsStub.slice(0, amount)
    cy.intercept('GET', '**/api/classifications/*', slicedClassifications)
}).as('classifications') });

As you can see this customcommand is intercepting 2 api request. Namely:

  1. ** /api/classifications
  2. ** /api/classifications/ *

The first interception is with [] The second intercecption needs exactly the same response buth without brackets []

But you already understand that both respons are now with brackets '[]'. I tried to make a second fixture file without [], but the the slice function is not working.

So I need: The second intercept like:

{
    "id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
    "name": "CypressTest",
    "userRole": "Editor",
    "childClassifications": []
}

But I get:

[{
    "id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
    "name": "CypressTest",
    "userRole": "Editor",
    "childClassifications": []
}]

How could I get this array back without []? Thankyou indeed!

UPDATE: I am trying now the following:

Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {

cy.fixture('ClassificationsStub.json').then(classificationsStub => {
    const slicedClassifications = classificationsStub.slice(0, amount)
    cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')


cy.fixture('ClassificationsStub.json').then(classificationsStub => {
    const slicedClassifications = classificationsStub.slice(0, amount)
    const arr2 = slicedClassifications
    const obj5 = Object.fromEntries(arr2)
    
   
    cy.intercept('GET', '**/api/classifications/*', obj5)
}).as('classification')});

But this give me a emtpy .json file. It the respons is not just ' {} '

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

There looks to be some brackets out of place in the code.

I would also recommend using different alias names, otherwise the calls to cy.wait('@classifications') may not work as you expect.

Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {

  cy.fixture('ClassificationsStub.json').then(classificationsStub => {

    const slicedClassifications = classificationsStub.slice(0, amount)
    cy.intercept('GET', '**/api/classifications', slicedClassifications)
      .as('classifications')

    const firstClassification = slicedClassifications[0]  // just first item
    cy.intercept('GET', '**/api/classifications/*', firstClassification)
      .as('firstClassification') 

  })
});
about 4 years ago · Santiago Trujillo Report

0

So in case, your data looks like this:

var data = [{
    "id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
    "name": "CypressTest",
    "userRole": "Editor",
    "childClassifications": []
}]

So to extract the data with just curly braces you can do data[0].

{
    "id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
    "name": "CypressTest",
    "userRole": "Editor",
    "childClassifications": []
}

Working example console screenshot:

console sccreenshot

about 4 years ago · Santiago Trujillo Report

0

Finally solved with:

Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {

cy.fixture('ClassificationsStub.json').then(classificationsStub => {
    const slicedClassifications = classificationsStub.slice(0, amount)
    cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')


cy.fixture('ClassificationsStub.json').then(classificationsStub => {
   
    cy.intercept('GET', '**/api/classifications/*', (req) => {
        const slicedClassifications = classificationsStub.slice(0, amount)
        var selectedClassification = req.url.replace(new RegExp('.*api/classifications/'), '');
        let foundClassification = FindChildClassification(selectedClassification, slicedClassifications);

        //If not found return first always
        req.reply({
                    statusCode: 200,
            body: foundClassification ?? slicedClassifications[0]
                })
    })
}).as('classification')

And

export function FindChildClassification(id, classificationArray) {
for (let i = 0; classificationArray.length - 1 >= i; i++) {
    if (classificationArray[i].id == id) {
        return classificationArray[i];
    } else {
        if (classificationArray[i].childClassifications.length > 0) {
            let childClassification = FindChildClassification(id, classificationArray[i].childClassifications);
            if (childClassification != null) { return childClassification }
        }
    }
}

return null;

}

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!