Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to save fixture from response provided in route2 #9190

Closed
mzedeler opened this issue Nov 13, 2020 · 1 comment
Closed

Unable to save fixture from response provided in route2 #9190

mzedeler opened this issue Nov 13, 2020 · 1 comment

Comments

@mzedeler
Copy link

The route2 reply handler doesn't allow any further commands, making it impossible to save fixtures, which is necessary for us to be able to use Cypress for mocking our backend.

Using the following example:

    cy.route2(matcher, request => request.reply(response => {
        const obj = {
          url: request.url,
          request: _pick(request, ['method', 'body']),
          response: _pick(response, ['body', 'headers', 'statusCode'])
        }
        cy.writeFile(
          path.join('cypress', 'fixtures', fixturePath, 'mock.json'),
          obj
        )
      })
    )

Current behavior

Using the snippet above causes this error:

Uncaught CypressError: The following error originated from your test code, not from Cypress.

  > A response callback passed to `req.reply()` threw an error while intercepting a response:

Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

  > `cy.visit()`

The cy command you invoked inside the promise was:

  > `cy.writeFile()`

Desired behavior

Make it possible to access the response outside route2, possibly by letting the handler inside reply yield it.

Test code to reproduce

See the snippet above.

Versions

5.6.0

@flotwig
Copy link
Contributor

flotwig commented Nov 13, 2020

Make it possible to access the response outside route2, possibly by letting the handler inside reply yield it.

There are two ways to do this:

  1. Aliasing - cy.route2(matcher).as('someAlias'), and then cy.wait('@someAlias') will yield an object with the required request and response objects - see https://docs.cypress.io/api/commands/route2.html#Using-the-yielded-object
  2. Using Javascript scopes -
    let request, response
    cy.route2(matcher, (req) => {
    	request = req
    	req.reply(res => {
    		response = res
    	})
    })

The route2 reply handler doesn't allow any further commands

This is an open feature request for this here: #7667 I am not sure if this will actually ever get added, since it makes it really easy to write non-deterministic tests, but arguably there should be a way to use things like cy.task from inside a route handler.

@flotwig flotwig closed this as completed Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants