Cypress - Passing fixture alias to intercept method in version 9 and above

769 views Asked by At

Could someone please help me with passing fixture alias to intercept method in Cypress v 9.0. I remember using the following syntax in Cypress ~4 with route method as cy.route("GET", "**/users/*", "@users") where @users is a fixture defined as cy.fixture('users.json').as('users')

Since the cy.route is deprecated and we switched to using cy.intercept, I cannot seem to find the right syntax for passing fixture alias to the intercept method. I would like to know if the following syntax is possible cy.intercept("GET", "**/users/*", "@users")

Any help is kindly appreciated. Thanks

1

There are 1 answers

1
Fody On

I don't think there's a configuration to use an aliased fixture with intercept.

You can however, use the fixture name explicitly

cy.intercept("GET", "**/users/*", {fixture:'users.json'})   // pattern for fixture use

Given the fixture is now named in the staticResponse object, you may be able to use the alias there (but I don't see it documented)

cy.intercept("GET", "**/users/*", {fixture:'@users'})     // may not work

If you have a specific need to use an aliased value, this pattern is equivalent

cy.get('@alias').then(data => {
  cy.intercept("GET", "**/users/*", {body:data})     // set data staticResponse body
})