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.

Dynamic alias for cy.intercept()

See original GitHub issue

What would you like?

By #3083, I have to distinguish each graphql request because, cypress groups requests which has same endpoint very often. To handle independently each request, we can use operationName for alias like below.

cy.intercept('POST', '/graphql', req => {
  req.alias = req.body.operationName;
});

cy.wait('@someAlias');

However, if same request is sent multiple time with different variables, it is grouped again. Therefore, I cannot distinguish these queries. To do this, I thought random string will work as a suffix to operationName like below.

cy.intercept('POST', '/graphql', req => {
  const randomString = generateRandom();
  req.alias = `${req.body.operationName}${randomString}`;
});

From above, each request differentiated even if it is same request. But, how can I wait? how can I store that dynamic alias? I mean this ${req.operationName}${randomString} alias. When I store this alias to outer variable, I cannot use that variable with wait syntax.

Is there other way?

Below solution is not working

let alias = '';
cy.intercept('POST', '/graphql', req => {
  const randomString = generateRandom();
  alias = `${req.body.operationName}${randomString}`
  req.alias = alias;
});
cy.wait(`@${alias}`);

Why is this needed?

I have to wait all graphql requests when I load or visit specific page.

Thanks for your reading.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
bahmutovcommented, May 7, 2021

@baeharam the problem here is that by the time .then() => cy.wait(@{alias}) runs the alias variable is still undefined. We first need to make sure the alias is set. Here is how you could do that

it('creates random alias', () => {
  let alias = ''
  cy.intercept('GET', '/todos', (req) => {
    alias = 'get-todos-' + Cypress._.random(1e6)
    req.alias = alias
  })

  cy.visit('/?delay=2400')

  // first, wait for the alias string to become define
  cy.wrap('the alias string')
    .should(() => {
      expect(alias, 'alias string').to.not.be.empty
    })
    .then(() => {
      cy.wait(`@${alias}`)
    })
})

First, we retry using .should(() => ...) until the alias is set. Then we have the .then callback - by the time it executes, the alias has been set and all is good. Here is this test in action

https://user-images.githubusercontent.com/2212006/117389908-294d9600-aebb-11eb-97c7-2edf2e496b05.mp4

2reactions
bahmutovcommented, May 7, 2021

So at this point I would ask myself - do you know what to expect? Maybe you want to check if the number of requests is > 10 and write that as an assertion? Maybe use something like network idle by looking at the timestamp of the last intercept? I have such example at https://glebbahmutov.com/blog/cypress-tips-and-tricks/#wait-for-network-idle

Read more comments on GitHub >

github_iconTop Results From Across the Web

intercept - Cypress Documentation
Use cy.wait() with aliasing an intercepted route to wait for the request/response cycle to complete. With URL. cy.
Read more >
Cypress cy.intercept Problems - Gleb Bahmutov
Unfortunately overwriting cy.intercept has a bug with aliases #9580, and thus we cannot always show when the intercept is registered.
Read more >
A Practical Guide to Intercepting Network Requests in Cypress
Our .intercept() command is now matching any request that contains the url /api/boards ... Using our matchedUrl alias that we assigned to our...
Read more >
cypress - cy.intercept() for post endpoints with query parameters
In the runner I can't see the alias assigned to endpoint as in the below screenshot. cy.intercept('POST', `https://api.dev.myapp.com/api/program ...
Read more >
Cypress - intercept Spy and stub network requests responses.
cy.intercept() is the successor to cy.route() as of Cypress 6.0.0. ... spying, dynamic stubbing, request modification, etc. cy.intercept(url, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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