Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
740 views
in Technique[技术] by (71.8m points)

cypress - Minimatch pattern for two paths where one is prefix of the other

I am writing an integration test with cypress and having trouble with minimatch pattern.

I have two endpoints that I need to stub. /users/1 and /users/1/profile.

The way I am trying to mock these two endpoints with cy.intercept() is the following. For the first url, /users/1, I tried cy.intercept('GET', '/users/1', {}).

For the secton url , /users/1/profile, I tried cy.intercept('GET', '/users/1/profile', {}).

The problem is that the first pattern intercepts twice.

Can I get some help on this?? Thanks.

question from:https://stackoverflow.com/questions/66056942/minimatch-pattern-for-two-paths-where-one-is-prefix-of-the-other

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I, too, fell into this problem when first using cy.intercept. The solution is to pass in a RouteMatcher object to the method. In particular, you'll want to use the last method signature from the image below:

usage

In the RouteMatcher object, you can specify a path property. Here's the description of the path property:

path property

In essence, using the path property of the RouteMatcher object does an exact match against the given string, whereas the url parameter in the 1st and 2nd method signatures does a substring match against the given string.

So what you'll want is:

cy.intercept(
    {method: 'GET', path: '/users/1'},
    {body: {}}
)

cy.intercept(
    {method: 'GET', path: '/users/1/profile'},
    {body: {}}
)

In my opinion, this slight change from Cypress between the cy.route and cy.intercept methods was weird and a bit unexpected on the first run-through.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...