-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Conversation
- provides additional signature for `cy.its()` - adds kitchen sink examples for path-based property access
Thanks for the contribution! Below are some guidelines Cypress uses when doing PR reviews.
PR Review ChecklistIf any of the following requirements can't be met, leave a comment in the review selecting 'Request changes', otherwise 'Approve'. User Experience
Functionality
Maintainability
Quality
Internal
|
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.
This might also need to be added to cy.invoke
, since its
and invoke
use the same code path for finding properties.
Test summaryRun details
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 |
Added better signatures for cy.invoke here too, curious what you think! 🤔
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.
Like it, I think this works just fine
/** | ||
* 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>> |
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.
these are redundant now, right?
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.
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 |
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.
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
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!
…On Tue, Mar 17, 2020 at 11:28 AM Nicholas Boll ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In cli/types/index.d.ts
<#6667 (comment)>:
> @@ -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
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']) // numbercy.wrap({ a: { b: 2 }}).it's('b') // errorcy.wrap({ a: { b: 2 }}).it's(['a', 'c']) // error
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#6667 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOVVUCSZD7F4ZPGXD2MY3CLRH6JKHANCNFSM4LDDAO4Q>
.
|
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.
…On Tue, Mar 17, 2020 at 11:53 AM Joseph Weissman ***@***.***> wrote:
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!
On Tue, Mar 17, 2020 at 11:28 AM Nicholas Boll ***@***.***>
wrote:
> ***@***.**** commented on this pull request.
> ------------------------------
>
> In cli/types/index.d.ts
> <#6667 (comment)>:
>
> > @@ -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
>
> 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']) // numbercy.wrap({ a: { b: 2 }}).it's('b') // errorcy.wrap({ a: { b: 2 }}).it's(['a', 'c']) // error
>
> —
> You are receiving this because you modified the open/close state.
> Reply to this email directly, view it on GitHub
> <#6667 (review)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AOVVUCSZD7F4ZPGXD2MY3CLRH6JKHANCNFSM4LDDAO4Q>
> .
>
|
Lodash's
I guess that's fair... Lodash supports it as well. As soon as you put a 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 |
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: cypress/packages/driver/src/cy/commands/connectors.coffee Lines 335 to 338 in 36866da
|
cy.its()
andcy.invoke()
with property pathscy.invoke()
User facing changelog
cy.its()
andcy.invoke()
(overloads for property-path arguments; strongly-typeinvoke
usingReturnType
)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 signatureits
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 wellHow has the user experience changed?
PR Tasks
cypress-documentation
?type definitions
?cypress.schema.json
?