-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
add matcher
option to cy.route - addresses #387
#3984
Conversation
matcher
option to cy.routematcher
option to cy.route - addresses #387
@jennifer-shehane where would I add a test for this? |
What should I do to elicit a response on this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll also need a PR in cypress-documentation to document the new option before this can be merged.
@@ -272,6 +272,7 @@ create = (options = {}) -> | |||
testStr(fullyQualifiedUrl, options.stripOrigin(fullyQualifiedUrl)) | |||
|
|||
xhrMatchesRoute: (xhr, route) -> | |||
return route.matcher(xhr, route) if _.isFunction(route.matcher) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests for the desired behavior?
The specs for cy.server, cy.route, etc. are all here: https://github.com/cypress-io/cypress/blob/develop/packages/driver/test/cypress/integration/commands/xhr_spec.coffee
The Cypress project for the driver is in You'll also need to set it up to compile your changes, instructions for building the driver are here: https://github.com/cypress-io/cypress/blob/develop/packages/driver/README.md#building Let me know if you have difficulty with this, it should just work. |
- matcher trumps url - non-func matcher throws error
Hey @flotwig, thanks for getting back to me! I added 4 tests. I also noticed that those tests failed because Please let me know if you need anything else on this PR. |
@flotwig I added the tests for What I found out, is that the Could you maybe give me some pointers to how I get access to the request body inside |
@godspeedelbow I took a look at your branch to see how the request body could be passed in, but I actually couldn't figure it out either. I added a (failing) test that ensures that
|
I think this happens because However, in Cypress one complication is that in However, in One option is: we don't pass the request So, as an alternative, what I started thinking instead is maybe we should pass In investigating this I ran into another wall and that is that the @flotwig Do you have any idea how we could do that? |
@godspeedelbow Sadly, I'm not too familiar with this part of the code, but maybe someone else will chime in. @cypress-io/test-runner, any thoughts? |
Closing this. I don't see how to this with current architecture. |
@godspeedelbow Hi, i think your solution is useful, why not reopen this PR? When using cypress to test an app with single api endpoint (i.e graphql),
If we can hook into this method, then we can use Waiting for your feedback :) |
Hi Xi! I am not entirely sure how the code works anymore. "It's complicated", is all I remember. I decided I am just going to wait for Cypress 2 which should land with better support for stubbing. Feel free to continue on this however! |
Addresses #387
Allows you to write routes like this:
Notes:
matcher
is an option that can be passed when setting up a route using thecy.route(options)
api.matcher
should be a function (if it's not a function, it's ignored) and it is passed two arguments:xhr
and the routeoptions
. The latter so that you don't have to rely on closure to validate your route, e.g. you could do something like thismatcher: (xhr, route) => xhr.method === route.method
matcher
should returntrue
if the route matches the xhr request, andfalse
if it doesn't. "truthy" (e.g. "it's a match!") is intepreted astrue
, "falsy" (e.g.undefined
) is interpreted asfalse
.matcher
takes precedence over normal route matching. If amatcher
function is passed, it will be evaluated and the normal matching is ignored. There is no fallback to the original route matching.To do:
ReferenceError: cy is not defined
, etc). Maybe somebody can tell me where to put the test and where the instructions are for running that test?