Skip to content

Improve types for cy.its() with property paths #6667

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

Merged
merged 10 commits into from
Mar 11, 2020

Conversation

CypressJoseph
Copy link
Contributor

@CypressJoseph CypressJoseph commented Mar 6, 2020

  • Provides type signatures for using cy.its() and cy.invoke() with property paths
  • Adds kitchen-sink examples for path-based property access for these methods
  • Provides more robust typing for cy.invoke()

User facing changelog

  • Provide more robust type signature for cy.its() and cy.invoke() (overloads for property-path arguments; strongly-type invoke using ReturnType)

Additional details

  • cy.its() is a wrapper around lodash's _.get, and so accepts a property path to glom into nested structures (my.nested.property), but this form wasn't given a signature
  • Note we can't really infer the type through the its when given a nested property. But no red squiggly anymore...
  • cy.invoke() uses the same strategy under the hood, so we provide property-path signatures for it as well

How has the user experience changed?

PR Tasks

  • Have tests been added/updated?
  • Has the original issue been tagged with a release in ZenHub?
  • Has a PR for user-facing changes been opened in cypress-documentation?
  • Have API changes been updated in the type definitions?
  • Have new configuration options been added to the cypress.schema.json?

- provides additional signature for `cy.its()`
- adds kitchen sink examples for path-based property access
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Mar 6, 2020

Thanks for the contribution! Below are some guidelines Cypress uses when doing PR reviews.

  • Please write [WIP] in the title of your Pull Request if your PR is not ready for review - someone will review your PR as soon as the [WIP] is removed.
  • Please familiarize yourself with the PR Review Checklist and feel free to make updates on your PR based on these guidelines.

PR Review Checklist

If any of the following requirements can't be met, leave a comment in the review selecting 'Request changes', otherwise 'Approve'.

User Experience

  • The feature/bugfix is self-documenting from within the product.
  • The change provides the end user with a way to fix their problem (no dead ends).

Functionality

  • The code works and performs its intended function with the correct logic.
  • Performance has been factored in (for example, the code cleans up after itself to not cause memory leaks).
  • The code guards against edge cases and invalid input and has tests to cover it.

Maintainability

  • The code is readable (too many nested 'if's are a bad sign).
  • Names used for variables, methods, etc, clearly describe their function.
  • The code is easy to understood and there are relevant comments explaining.
  • New algorithms are documented in the code with link(s) to external docs (flowcharts, w3c, chrome, firefox).
  • There are comments containing link(s) to the addressed issue (in tests and code).

Quality

  • The change does not reimplement code.
  • There's not a module from the ecosystem that should be used instead.
  • There is no redundant or duplicate code.
  • There are no irrelevant comments left in the code.
  • Tests are testing the code’s intended functionality in the best way possible.

Internal

  • The original issue has been tagged with a release in ZenHub.

@CypressJoseph CypressJoseph requested a review from flotwig March 6, 2020 16:02
flotwig
flotwig previously requested changes Mar 6, 2020
Copy link
Contributor

@flotwig flotwig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might also need to be added to cy.invoke, since its and invoke use the same code path for finding properties.

@cypress
Copy link

cypress bot commented Mar 6, 2020



Test summary

6966 0 97 0


Run details

Project cypress
Status Passed
Commit bd88020
Started Mar 10, 2020 7:43 PM
Ended Mar 10, 2020 7:48 PM
Duration 05:14 💡
OS Linux Debian - 10.0
Browser Multiple

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@CypressJoseph CypressJoseph changed the title Improve types for cy.its() with property paths WIP Improve types for cy.its() with property paths Mar 9, 2020
@CypressJoseph CypressJoseph requested a review from flotwig March 9, 2020 17:18
@CypressJoseph CypressJoseph changed the title WIP Improve types for cy.its() with property paths Improve types for cy.its() with property paths Mar 9, 2020
@CypressJoseph CypressJoseph dismissed flotwig’s stale review March 9, 2020 20:33

Added better signatures for cy.invoke here too, curious what you think! 🤔

@CypressJoseph CypressJoseph requested a review from bahmutov March 11, 2020 14:34
Copy link
Contributor

@bahmutov bahmutov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like it, I think this works just fine

Comment on lines 1087 to 1092
/**
* Invoke a function in an array of functions.
* @see https://on.cypress.io/invoke
*/
invoke<T extends (...args: any[]) => any, Subject extends T[]>(index: number): Chainable<ReturnType<T>>
invoke<T extends (...args: any[]) => any, Subject extends T[]>(options: Loggable, index: number): Chainable<ReturnType<T>>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are redundant now, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these signatures are capturing a slightly different case than the others (i.e., index-access into array of functions, which lets us type the return value somewhat more narrowly; note we're specifying that Subject extends T[] which isn't quite like the other forms?)

@@ -1099,6 +1111,7 @@ declare namespace Cypress {
* cy.wrap({foo: {bar: {baz: 1}}}).its('foo.bar.baz')
*/
its<K extends keyof Subject>(propertyName: K, options?: Loggable): Chainable<Subject[K]>
its(propertyPath: string, options?: Loggable): Chainable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changelog said the typings of its is now more robust. This solution returns any for a deep path, and worse yet, loses all type safety. If you misspell a property, there is no longer an error. The subject will silently become an any. That's less robust.

Would the Cypress team be open to allowing an array of keys for a path? Doing that would allow type safe deeply nested paths.

cy.wrap({ a: { b: 2 }}).it's(['a', 'b']) // number
cy.wrap({ a: { b: 2 }}).it's('b') // error
cy.wrap({ a: { b: 2 }}).it's(['a', 'c']) // error

@CypressJoseph
Copy link
Contributor Author

CypressJoseph commented Mar 17, 2020 via email

@CypressJoseph
Copy link
Contributor Author

CypressJoseph commented Mar 17, 2020 via email

@NicholasBoll
Copy link
Contributor

NicholasBoll commented Mar 17, 2020

I love this idea! 👍 We initially explored something like this because it could be strongly-typed, but the core problem is that under the hood we're still using lodash's _.get. If and when we have more control over the shape of this method I'd definitely be in favor of moving forward with this more path-oriented approach!

Lodash's get method supports arrays for paths: https://lodash.com/docs/4.17.15#get. The type definitions support paths, too. I'd have to test it, but probably no code would have to change.

Thanks for pointing this out, by the way. We still want to be able to type simple property paths through (i.e., cy.wrap({ a: 1 }).its('a')), as there's a fallback that still permits 'a.nested.property' with a weaker type.

I guess that's fair... Lodash supports it as well. As soon as you put a . in there, lodash's types fall back to any. This loses type safety on the path. I'd say "with no type safety" vs "with a weaker type".

The tradeoff is 100% compatibility with Javascript code or stronger type checking. I lean toward stronger type checking, but I can understand the position. Lodash falls back to any. I just like when Typescript saves me from simple typos. The error is explicit. I'd rather have an error than silently losing all type safety with an any. I don't even have a local way to prevent the any fallback.

@NicholasBoll
Copy link
Contributor

I've added an issue: #6758

I verified that array of keys is not allowed by Cypress (it does runtime type checks), but internally path traversal turns the string into an array anyway:

if _.isString(str)
paths = str.split(".")
else
paths = [str]

@emilyrohrbough emilyrohrbough deleted the issue-6431-its-compound-props-types branch August 1, 2024 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

.its command with compound properties failing TypeScript check
4 participants