1

I am trying to modify an existing fixture and then override a response with the modified fixture.

I'm able to override the response with predefined fixture or modify the actual response, but I cant do them both at the same time.

For example the actual response can be modified:

 cy.intercept("POST", "response", (req) => {
  req.continue((res) => {
    res.body.result..... = false;
    res.send();
  });
});

Also I am using this to override responses using a fixture:

Cypress.Commands.add("override", (url: string, fixture: string) => {
  cy.intercept(
    'url',
    fixtureName.json
  );
});

I can use cy.readFile() to read the fixture and cy.writeFile() to modify it but I do not want to use this solution.

Thank you in advance

Fody
  • 23,754
  • 3
  • 20
  • 37

2 Answers2

0

You can use the .fixture() to load your fixture file and then follow it with a .then() to modify the data and have it as part of your intercept response.

cy.fixture('fruit.json').then((data) => {
  data.fruit = 'Kiwi'
  cy.intercept('GET', '/fruit', fruit).as('getFruit')
})

// test code to trigger request
cy.wait('@getFruit').then(({ request }) => {
  expect(request.body.fruit).to.eq('Kiwi')
})
jjhelguero
  • 2,281
  • 5
  • 13
0

You are perhaps looking to "merge" response and fixture in some way using the spread operator?

cy.fixture('my-fixture.json').then(fixture => {

  cy.intercept("POST", "response", (req) => {
    req.continue((res) => {
      res.body = {
        ...fixture,              // mostly fixture properties
        result: res.body.result  // except for "result"
      }
      res.send();
    })
  })
})

Or

cy.fixture('my-fixture.json').then(fixture => {

  cy.intercept("POST", "response", (req) => {
    req.continue((res) => {
      res.body = {
        ...res.body,             // mostly res properties
        result: fixture.result   // except "result" from fixture
      }
      res.send();
    })
  })
})
Fody
  • 23,754
  • 3
  • 20
  • 37