let say I have this setting.json
{ data: { isDark: false } } 
and I intercept it like so
cy.intercept("api/setting", {
    fixture: `setting.json`,
 })
it worked.
But there's no way I want to create one file for one new property in the setting.json or when isDark is true. So how can I change the property values of the setting.json in my test?
I tried
cy.intercept("api/setting", (req) => {
 const settingFixture = await cy.fixture('setting.json')
 req.continue((res) => {
  res.send(settingFixture.map((object)=>({...object, isDark: true}))
 })
 cy.visit('some where')
})
but it doesn't work.
According to Cypress documentation about fixture command (https://docs.cypress.io/api/commands/fixture#Modifying-fixture-data-before-using-it), I think you can try something like this:
cy.fixture('setting.json').then(settingFixture => {
    
    // Update your JSON object according to your context
    // ...
    // Stub your response with this JSON object updated
    cy.intercept("api/setting", settingFixture)
});
// Navigate to your URL
cy.visit('some where')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With