Configure cy.intercept to Only Intercept a Network Request Once

Filip Hric
InstructorFilip Hric
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

When testing certain user stories, you may want to start with a clean state, but then return to the initial page and see the data you have created. This can be problematic if you are stubbing your requests. Luckily, you can limit how many times you a request should be intercepted.

Instructor: [0:00] I want to create a test for a user story where a user would create their first board. I'll choose the Selector Playground, find my selector and then paste it inside my code and then type the name of the new board. I'll type in "Enter" to hit the Enter key, save my test, and I can see that it's doing what I want.

[0:28] After that, I want to continue with my test and go back home and create another board. I will copy my selector and click on it. When I now save my test, I can see that my board is not appearing in the list, and that is because I'm still intercepting my /api/boards request. To fix that, I can specify how many times should this request be intercepted.

[0:56] I'll change these first two arguments into an object. The first one will be method. The second parameter will be URL. I'll add a third parameter, which will be timesOne. This will specify how many times I want to intercept this command and change its response body. When I now save my test, I can see that my /api/boards is intercepted only once.

egghead
egghead
~ 8 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today