Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Stub Network Requests in a Cypress Test


    To keep our tests fast and easily repeatable, it makes sense to create many integration tests and fewer full end-to-end tests. In this lesson, we’ll see how to stub out network requests to create repeatable integration tests by ensuring our application runs in a known state for our tests.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: I have my application and Cypress both running locally. I've created this spec file with a single test. The test visits the application and asserts that the list todos has a length of four.

    Let's run the test and see if it passes. I'll switch to the Cypress UI and click on this new spec file, app-init.spec.js.

    We'll see when this runs it visits our page and makes our API call to the todos endpoint. Our list does in fact have a length of four.

    While I'm in this test, I'm going to come over to the app. I'm going to add a new todo right inside Cypress.

    We'll see that it makes my post. I get a inaudible back. My new todo is added to the list.

    Now I'm going to rerun this existing test. Let's see what happens.

    We'll see here that our test fails because it's expecting a length of four but it got five. The problem here is that we're using the actual database. When we make changes to that database and this API call is made to load the todos, it's going to load whatever's in the database at the time, so our test isn't really all that repeatable, especially if we're using the application locally for development.

    Let's see how we can stub out this API call to make our test more reliable and repeatable. Back in our code, I'm going to update the test.

    Before our call to cy.visit, I'm going to add a call to cy.server. This is going to start a server that's going to allow us to keep track of and stub out our API calls.

    After our call to cy.server, I'm going to call cy.route. Route's going to take an argument for a method. Its second argument here is going to be the URL, which is our api/todos endpoint.

    Then we're going to give it a response. For right now, we'll just pass it an empty array.

    When this test runs, it's going to intercept our get call to api/todos. It's going to respond to our application with this empty array. Let's save the test and see it run.

    Our test is still failing, but now it's failing for a new reason. This time, it's looking for a length of four, but it got zero. If we look at our application in the preview pane, we'll see that there are no todos in the list. This is because our XHR call was stubbed and we gave it an empty array as the response.

    We can see over here in the command log that our XHR call that was being logged out before now says "stub" next to it. We know this is using our stubbed call. Now all we have to do is update our return value to match our expectation.

    Back in the code, I'm going to update this array. I'm going to give it four todo objects. Each one of these will have an ID and a name and an isComplete value.

    We'll just update these IDs and the names. They can all be incomplete. I'll save this again, switch back to our test.

    This time when it runs, we'll see that we get our mocked API response with our one, two, three, four values. Our test is now passing.

    If I do the same thing again and I add a new todo, this is going to make our actual API call, because we haven't stubbed out the post. This has been saved to our database. Our new todo is showing here.

    If I start this test from the beginning, we'll see we've been isolated from what's in our database using this stubbed API call.