https://cypress.io logo
cy.intercept routeMatcher with query object issue
# i-need-help
t
Does anyone precisely know how query param object matching in the
cy.intercept
routeMatcher works. The docs aren't very specific but it seems that if you use strings there, they will do partial/wildcard/contains matching and not exact string matching. The reason I'm asking is because I'm testing a suggestion feature with two filter fields (first-name and last-name). Changing each field will trigger a call to the suggestion API (actually while typing but
cy.type
is fast enough to only trigger it once per field) which I need to intercept. I tried using this code (simplified):
Copy code
const pathname = `/suggestions`
cy.intercept({
  pathname,
  query: {
    first_name: 'John',
    last_name: '',
  },
}).as('first-name-search')

cy.intercept({
  pathname,
  query: {
    first_name: 'John',
    last_name: 'Doe',
  },
}).as('last-name-search')

cy.get('input[name=first_name]').type('John')
cy.wait('@first-name-search')

cy.get('input[name=last_name]').type('Doe')
cy.wait('@last-name-search')
... but then the
first-name-search
intercepts both API calls and the empty
last_name
query property seems to match twice. So even though there are only 2 API calls, I get three matches in the routes panel. I can't really use a URL string matcher here as the API library sometimes messes with the query param order. The only thing that got it working for me is to replace the empty string with a very restrictive Regex:
/^$/
. Is there no better way than this?
n
You are right it match using contains not the exact string. I had the similar problem and I matched it using that type of regex. Docs say that route matcher uses Cypress.minimatch and there is an easier way to test strings in the cypress running browser console https://docs.cypress.io/api/utilities/minimatch I would try to play with this