Alicia Y
Alicia Y

Reputation: 387

modify fixture data in cypress and intercept it

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.

Upvotes: 4

Views: 3547

Answers (1)

Nicolas G.
Nicolas G.

Reputation: 255

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')

Upvotes: 8

Related Questions