最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - modify fixture data in cypress and intercept it - Stack Overflow

programmeradmin10浏览0评论

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.

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.

Share Improve this question asked Jan 6, 2022 at 9:25 Alicia YAlicia Y 3871 gold badge6 silver badges16 bronze badges 1
  • I think part of your answer in this video youtube./watch?v=vlLLi5N4h78, check it out – Evgenii Bazhanov Commented Jan 6, 2022 at 9:50
Add a ment  | 

1 Answer 1

Reset to default 8

According to Cypress documentation about fixture mand (https://docs.cypress.io/api/mands/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')
发布评论

评论列表(0)

  1. 暂无评论