Skip to content

times option of cy.intercept doesn't work with req.reply #17139

Closed
@sainthkh

Description

@sainthkh

Current behavior

I've written the code below when fixing #16821.

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="/node_modules/jquery/dist/jquery.js"></script>
  </head>
  <body>
    <button id="request">request</button>
    <div id="result"></div>
    <script type="text/javascript">
      $(function(){
        $("button#request").click(function(){
          $.ajax({
            method: "POST",
            url: "/post-only",
            data: JSON.stringify({client: 'data'})
          })
          .then((data) => {
            $('#result').text(data)
          })
        })
      })
    </script>
  </body>
</html>
it('works with reply', () => {
  cy.intercept({
    method: 'POST',
    times: 1,
    url: '/post-only',
  },
  (req) => {
    req.reply('stubbed data')
  }).as('interceptor')

  cy.visit('fixtures/request.html')

  cy.get('#request').click()
  cy.get('#result').should('contain', 'stubbed data')

  cy.get('#request').click()
  cy.get('#result').should('contain', 'client') // Fail
})

But it failed because our times counter only works with before:request event. But reply doesn't fire it and it only fires response-related ones.

Desired behavior

The test should pass.

Test code to reproduce

See above.

Versions

Current Cypress 7.6.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions