question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow dynamically matching requests with a function

See original GitHub issue

To allow highly flexible matching of which requests you want to stub, we could add the option to accept a function instead of a fixed method/URL. This would recieve the request object (or some wrapper around it). The motivation is that I have and SPA that uses a GraphQL backend where all requests have the same URL and I want to filter them by the query name which is sent as a parameter in the POST body.

cy.route({
  matcher: (request) => !! request.body.match(//)
})

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:45
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

19reactions
nazarcommented, Jan 30, 2019

Thank you for the update @jennifer-shehane

I’ve been able to find a work-around that provides me with the required functionality. I feel it’s a bit hacky but it works for now.

My support/index.js contains:

beforeEach(() => {
  cy
    .server()
    .route({
      method: 'POST',
      url: '/api/graphql' <---- this is our graphQL endpoint
    })
    .as('graphqlRequest');
});

And the following in my support/commands.js

Cypress.Commands.add('waitForQuery', operationName => {
  cy.wait('@graphqlRequest').then(({ request }) => {
    if (request.body.operationName !== operationName) {
      return cy.waitForQuery(operationName); <---- this is the hacky bit
    }
  });
});

The above can be used as follows:

      it('Should Foo Bar', () => {
        cy
          .waitForQuery('fooQuery')

          .get('[data-cy=bar]')
          .should('exist')
       
           // can also be used to check XHR payloads
          .waitForQuery('fooQuery')
          .its('request')
          .then(({ body: { variables: { search: { foo } } } }) => expect(foo).to.equal(1))
       });

HTH anybody with the same requirements.

12reactions
godspeedelbowcommented, Apr 17, 2019

Opened a PR for this feature request after spending way to much time trying to hack it in in userland. This should be supported out of the box IMO.

https://github.com/cypress-io/cypress/pull/3984

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Dynamic Parameters for Intelligent Mock Responses
Dynamic parameters enable you to pass values from incoming requests to responses, as well as execute logic within your responses.
Read more >
Using Parameters of One Request to Dynamically Change the ...
What I probably need to do is do a findAll() on the requests, find the one that matches with the request, and retrieve...
Read more >
Dynamically Route Viewer Requests to Any Origin Using ...
Now you can programmatically define the origin based on logic in your Lambda function. This enables you to route requests to different ...
Read more >
Dynamically Selecting API Gateway Back Ends Based on ...
A common requirement is to route requests sent to the same API gateway to different back ends based on elements in the request....
Read more >
Dynamic methods for matching requests to suppliers in ...
Peer-to-peer transportation platforms dynamically match requests (e.g., a ride, ... cannot be certain that a supplier will accept an offered request.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found