Configure cy.intercept to Only Intercept a Network Request Once

Filip Hric
InstructorFilip Hric

Share this video with your friends

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.