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

Typings for cy.route inconsistent with documentation #1831

Closed
bsmithEG opened this issue May 30, 2018 · 4 comments · Fixed by #1874
Closed

Typings for cy.route inconsistent with documentation #1831

bsmithEG opened this issue May 30, 2018 · 4 comments · Fixed by #1874
Assignees

Comments

@bsmithEG
Copy link

bsmithEG commented May 30, 2018

Is this a Feature or Bug?

(documentation) bug

Current behavior:

Docs (or typings) are wrong

Desired behavior:

Both should be consistent

Steps to reproduce:

The docs for cy.route with stubbing are inconsistent with the typings published with cypress. Examples:

Docs:

cy.route('**/users', [
  {id: 19, name: 'Laura'},
  {id: 20, name: 'Jamie'}
])

const commentsResponse = (routeData) => {
  //routeData is a reference to the current route's information
  return {
    data: someOtherFunction(routeData)
  }
}

cy.route('POST', '**/comments', commentsResponse)

And the typings for cy.route:

route(url: string | RegExp, response?: string | Response): Chainable<null>
route(method: string, url: string | RegExp, response?: string | Response): Chainable<null>
route(fn: () => RouteOptions): Chainable<null>
route(options: Partial<RouteOptions>): Chainable<null>

Also, it seems that Cypress.Response isn't really want is specified in the docs. At least, if this is what's required, a bunch of props that probably shouldn't be necessary for this are required.

  interface Response {
    allRequestResponses: any[]
    body: any
    duration: number
    headers: { [key: string]: string }
    isOkStatusCode: boolean
    redirectedToUrl: string
    requestHeaders: { [key: string]: string }
    status: number
    statusText: string
  }

Versions

Cypress 3.0.0

@jennifer-shehane
Copy link
Member

Sorry, could you be more specific about where exactly the docs/typings differ? Is there an error when you run the code above?

@jennifer-shehane jennifer-shehane added topic: typescript stage: needs information Not enough info to reproduce the issue labels May 30, 2018
@bsmithEG
Copy link
Author

bsmithEG commented May 30, 2018

The typescript compiler has an error, and thus there's an error reflected in the IDE. I can get around this by ignoring it or typecasting around, but the point is that it's wrong unless the function isn't meant to be used in the way the docs specify.

There error I get in Cypress (but this just comes from the typescript preprocessor):

const campaignResponse = {...}
cy.route("POST", /twreceiver/, campaignResponse).as("campaignResponse")
[tsl] ERROR in /Users/rsmith/project/specs/test.spec.ts(61,40)
TS2345: Argument of type '{ campaignResponses: { campaignName: string; messages: { promotedItems: { type: string; name: str...' is not assignable to parameter of type 'string | Response'.
Type '{ campaignResponses: { campaignName: string; messages: { promotedItems: { type: string; name: str...' is not assignable to type 'Response'.
Property 'allRequestResponses' is missing in type '{ campaignResponses: { campaignName: string; messages: { promotedItems: { type: string; name: str...'.

@bahmutov
Copy link
Contributor

Hmm, I have to look at this, probably missing "partial"

@bahmutov bahmutov self-assigned this May 30, 2018
@no23reason
Copy link

no23reason commented Jun 4, 2018

@bahmutov IMO it is not missing Partial. According to the docs you can pass arbitrary object as the response (see the first example of the code):

cy.server()
cy.route('https://localhost:7777/users', [{id: 1, name: 'Pat'}])

Therefore I believe the typings should look something like this:

route(url: string | RegExp, response?: string | Response | object /* or any or unknown in TS 3.0 */): Chainable<null>
route(method: string, url: string | RegExp, response?: string | Response | object /* or any or unknown in TS 3.0 */): Chainable<null>
route(fn: () => RouteOptions): Chainable<null>
route(options: Partial<RouteOptions>): Chainable<null>

Also, maybe the Response could be removed from the possible types here or at least like you are saying made Partial.

bahmutov added a commit that referenced this issue Jun 4, 2018
brian-mann pushed a commit that referenced this issue Jun 5, 2018
* types: add object to routes, close #1831

* types: remove Response type from cy.route, it belongs only in cy.request

* add example to cy.route doc

* types: add PATCH method, same as pull request #1778
@jennifer-shehane jennifer-shehane removed stage: needs information Not enough info to reproduce the issue labels Jan 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants