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

intercept()'s minimatch glob-matching against routeMatcher url property no longer works #9379

Closed
knotthere opened this issue Nov 30, 2020 · 16 comments · Fixed by #14241
Closed
Assignees
Labels
cy.intercept() type: unexpected behavior User expected result, but got another

Comments

@knotthere
Copy link

Current behavior

In Cypress 4.12.1, matching a route (cy.route()) with a url property that contains a string with minimatch syntax (*) works.

In Cypress 6.0.0, using cy.intercept() with a routeMatcher.url that also contains minimatch syntax fails to match.

The help at https://docs.cypress.io/api/commands/intercept.html#routeMatcher-RouteMatcher says:

All properties are optional. All properties that are set must match for the route to handle a request. If a string is passed to any property, it will be glob-matched against the request using minimatch.The available routeMatcher properties are listed below:

Desired behavior

Glob-matching should succeed with string properties passed to cy.intercept() in a routeMatcher.

Test code to reproduce

Versions

@knotthere
Copy link
Author

I saw this, but I'm not sure if dropping minimatch support was intentional:
#9340 (comment)

@flotwig
Copy link
Contributor

flotwig commented Nov 30, 2020

@knotthere url is matched as a substring or via minimatch. Can you share your test code that is not working please?

@knotthere
Copy link
Author

@flotwig - Thank you for the followup and confirmation minimatch should still be supported. I will circle back on my code and provide a repeatable example (if this was not simply user-error on my part.)

@bahmutov
Copy link
Contributor

Ok, I added example in https://github.com/bahmutov/cy-intercept-example - it can be tricky if the URL has query parameters.

// to match "https://jsonplaceholder.cypress.io/users?_limit=3"
cy.intercept('**/users?*').as('users')
cy.get('#load-users').click()
cy.wait('@users')

Had to do a few tries in the DevTools console like this until found true

Cypress.minimatch('https://jsonplaceholder.cypress.io/users?_limit=3', '**/users', {debug: true})

@Pirlouit
Copy link

Pirlouit commented Dec 1, 2020

Seems to be a duplicate of #9305

@CSchulz
Copy link

CSchulz commented Dec 1, 2020

I am struggling with the same issue, the minimatch pattern does not work for intercept:

  Cypress.minimatch('http://localhost:4200/product/api/5/vehicles?producer=0005&type=450', '**/product/api/*/vehicles?*', {debug: true})
> true

  cy.intercept({
    method: 'GET',
    url: '**/product/api/*/vehicles?*',
  }).as('search');
> timeout while waiting

@knotthere
Copy link
Author

knotthere commented Dec 1, 2020

@Pirlouit - Yes, I began converting my url's from minimatch format to RegEx to work around this, but it sounds like minimatch glob-matching should still work with intercept(), but it does not always.

@Pirlouit
Copy link

Pirlouit commented Dec 1, 2020

Hey @knotthere,
Yes, it definetely should. This is a bug from v6.0. I do not doubt it will be fixed in the next release.
My work around is for the mean time.

@jarretmoses
Copy link

jarretmoses commented Dec 3, 2020

I think this is related/a dupe to #9340

@jennifer-shehane
Copy link
Member

Reproducible Example

// ✅ passes
it('cy.route() passes', () => {
  cy.server()
  cy.route('/users/search*').as('getUrl')
  cy.visit('https://example.com')
  cy.window().then((win) => {
    const xhr = new win.XMLHttpRequest()
    xhr.open('GET', '/users/search?=Sarah')
    xhr.send()
  })
  cy.wait('@getUrl')
})

// ❗️ fails
it('cy.intercept() fails', () => {
  cy.intercept('/users/search*').as('getUrl')
  cy.visit('https://example.com')
  cy.window().then((win) => {
    const xhr = new win.XMLHttpRequest()
    xhr.open('GET', '/users/search?=Sarah')
    xhr.send()
  })
  cy.wait('@getUrl')
})

// ✅ passes
it('cy.intercept() with pathname passes', () => {
  cy.intercept({
    method: 'GET',
    pathname: '/users/search*'
  }).as('getUrl')
  cy.visit('https://example.com')
  cy.window().then((win) => {
    const xhr = new win.XMLHttpRequest()
    xhr.open('GET', '/users/search?=Sarah')
    xhr.send()
  })
  cy.wait('@getUrl')
})

Screen Shot 2020-12-07 at 6 04 43 PM

@flotwig
Copy link
Contributor

flotwig commented Dec 9, 2020

The main issue here seems to be that cy.intercepts behavior differs from cy.route.

cy.route matches full URLs via minimatch, but if that fails, it will test against the pathname as well, as a convenience.

// ✅ matches http://example.com/foo/1
cy.route('http://example.com/foo/*')
// ✅ matches http://example.com/foo/1
cy.route('/foo/*')

As of 6.1.0, cy.intercept only matches full URLs via minimatch - you must explicitly match against the pathname property if you want to match on pathname.

// ❌ does not match http://example.com/foo/1
cy.intercept('/foo/*')
// ✅ matches http://example.com/foo/1
cy.intercept({ pathname: '/foo/*' })
// ✅ matches http://example.com/foo/1
cy.intercept('http://example.com/foo/*')
// ✅ matches http://example.com/foo/1
cy.intercept('**/foo/*')

Potentially, cy.intercept could be updated to match the old behavior:

// could be updated to match against pathname or full URL, like cy.route
cy.intercept('/foo/*')
// would still only match against full URL
cy.intercept({ url: 'http://example.com/foo/*' })
// would still only match against pathname
cy.intercept({ pathname: '/foo/*' })

@jennifer-shehane
Copy link
Member

We have most of the examples in our docs passing the pathname as the main argument for matching in cy.intercept() which is another reason I think no one expected this to be the new behavior.

Screen Shot 2020-12-10 at 3 49 53 PM

A lot less examples showing the 'correct' way in 6.1.0.

Screen Shot 2020-12-10 at 3 51 50 PM

Screen Shot 2020-12-10 at 3 51 57 PM

@jennifer-shehane
Copy link
Member

We are planning to update cy.intercept() to match the old behavior like cy.route() matching as Zach showed above.

@cypress-bot cypress-bot bot added stage: work in progress There is an open PR for this issue [WIP] stage: needs review The PR code is done & tested, needs review and removed stage: ready for work The issue is reproducible and in scope stage: work in progress There is an open PR for this issue [WIP] stage: needs review The PR code is done & tested, needs review labels Dec 18, 2020
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress There is an open PR for this issue [WIP] labels Dec 21, 2020
@cypress-bot cypress-bot bot added stage: pending release There is a closed PR for this issue and removed stage: needs review The PR code is done & tested, needs review labels Dec 21, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 21, 2020

The code for this is done in cypress-io/cypress#14241, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@knotthere
Copy link
Author

Thank you for addressing this. I await a new release so I can complete my port to Cypress 6.x.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 21, 2020

Released in 6.2.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v6.2.0, please open a new issue.

@cypress-bot cypress-bot bot removed the stage: pending release There is a closed PR for this issue label Dec 21, 2020
@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Dec 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cy.intercept() type: unexpected behavior User expected result, but got another
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants