Search code examples
javascriptautomationcypresscypress-intercept

cy.wait() timed out waiting 5000ms for the 1st request to the route when using cypress intercept


I'm attempting to mimic Cypress intercept from a youtube tutorial but for some reason mine isn't working.

Here's the code identical to the tutorial

   it.only('test api with simple intercept stubbing', () => {

        cy.visit('https://dummyapi.io/explorer');

        cy.intercept({
            path: 'data/v1/post/60d21af267d0d8992e610b8d/comment?limit=10'
        }).as('comments')

        cy.get('.flex > :nth-child(5)').click();
        cy.wait('@comments').then(intercept => {
            cy.log(JSON.stringify(intercept.response.body));
            expect(intercept.response.body.limit).equal(10)
        })
    })

Looking in the cypress debug console I can actually see the comments element that was clicked but it just hangs on the cy.wait

enter image description here

So it's navigating to the page, clicking 'comments', but then it just hangs on cy.wait and never asserts a successful GET. Any help would be greatly appreciated.


Solution

  • When you use the path option in cy.intercept() RouteMatcher object it needs to have a leading path separator /.

    cy.intercept({
        path: '/data/v1/post/60d21af267d0d8992e610b8d/comment?limit=10'
    }).as('comments')
    

    enter image description here