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

misc: update a few cypress types + convert internal driver tests to TypeScript #31055

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from

Conversation

jennifer-shehane
Copy link
Member

@jennifer-shehane jennifer-shehane commented Feb 7, 2025

Additional details

Converting some driver tests to TypeScript (more to come!).

It's interesting because I'm finding our own types that we expose to users and our internal ones are not ideal to work with ourselves.

This looks like a lot of changes, but it's mostly guarding against possible undefined variables or being more specific around an HTML Element type so that we can call a method that's available on that element.

Steps to test

Tests should pass

PR Tasks

@jennifer-shehane jennifer-shehane changed the title misc: update a few cypress types + convert internal driver tests to T… misc: update a few cypress types + convert internal driver tests to TypeScript Feb 7, 2025
@jennifer-shehane jennifer-shehane self-assigned this Feb 7, 2025
@@ -756,8 +756,10 @@ declare namespace Cypress {
getCoordsByPosition(left: number, top: number, xPosition?: string, yPosition?: string): number
getElementPositioning(element: JQuery | HTMLElement): ElementPositioning
getElementAtPointFromViewport(doc: Document, x: number, y: number): Element | null
getElementCoordinatesByPosition(element: JQuery | HTMLElement, position: string): ElementCoordinates
getElementCoordinatesByPosition(element: JQuery | HTMLElement, position?: string): ElementCoordinates
Copy link
Member Author

Choose a reason for hiding this comment

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

position is optional

Comment on lines +761 to +762
getHostContenteditable(element: HTMLElement): HTMLElement
getSelectionBounds(element: HTMLElement): { start: number, end: number }
Copy link
Member Author

Choose a reason for hiding this comment

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

its pretty strange that we make all of these methods public, but - that's how it's setup, so I added additional types in the public types although they're undocumented.

@@ -167,7 +168,14 @@ describe('src/cy/commands/actions/check', () => {
it('can forcibly click even when being covered by another element', () => {
const checkbox = $('<input type=\'checkbox\' />').attr('id', 'checkbox-covered-in-span').prependTo($('body'))

$('<span>span on checkbox</span>').css({ position: 'absolute', left: checkbox.offset().left, top: checkbox.offset().top, padding: 5, display: 'inline-block', backgroundColor: 'yellow' }).prependTo($('body'))
$('<span>span on checkbox</span>')
Copy link
Member Author

@jennifer-shehane jennifer-shehane Feb 7, 2025

Choose a reason for hiding this comment

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

This kept erroring not wanting the object.

@@ -282,7 +283,7 @@ describe('src/cy/commands/actions/click', () => {
const button = cy.$$('#button')

cy.get('#button').click().then(($button) => {
expect($button).to.match(button)
expect($button[0]).to.eq(button[0])
Copy link
Member Author

Choose a reason for hiding this comment

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

to.match is only meant to compare via Regex, but we were using this in a lot of places to check equality of elements. It did seem to be passing anyways somehow.

@@ -556,7 +557,7 @@ describe('src/cy/commands/actions/click', () => {
attachMouseHoverListeners({ btn, span })

btn.html('')
btn.attr('disabled', true)
btn.prop('disabled', true)
Copy link
Member Author

Choose a reason for hiding this comment

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

disabled with value should be set via prop

@@ -1280,7 +1282,7 @@ describe('src/cy/commands/actions/trigger', () => {
const { lastLog } = this
const { fromElWindow } = Cypress.dom.getElementCoordinatesByPosition($btn)

expect(lastLog.get('coords')).to.deep.eq(fromElWindow, 'x', 'y')
expect(lastLog.get('coords')).to.deep.eq(fromElWindow)
Copy link
Member Author

Choose a reason for hiding this comment

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

This doesn't accept more than 1 argument and what even were these extra arguments doing - nothing...

@@ -766,6 +772,7 @@ describe('src/cy/commands/actions/type - #type', () => {
.then(() => {
expect(cy.timeout).to.be.calledWith(5 * 8, true, 'type')

// @ts-expect-error - TODO: Get this to use the internal Keyboard types
Copy link
Member Author

Choose a reason for hiding this comment

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

I have no idea why some specfiles don't seem to be picking up the internal types I defined for some Cypress definitions.

Comment on lines +3202 to +3204
// @ts-expect-error - TODO: Not sure how to handle this
const spyTableName = cy.spy(top.console, 'group')
// @ts-expect-error - TODO: Not sure how to handle this
Copy link
Member Author

Choose a reason for hiding this comment

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

Just complaining that window doesn't have console on it.

/// <reference types="chai-subset" />
/// <reference types="cypress" />

/// <reference path="../../types/internal-types.d.ts" />
Copy link
Member Author

Choose a reason for hiding this comment

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

I have a feeling there's something extra I need to do to get our internal types in our cypress tests. Sometimes they work and sometimes they don't though.

Comment on lines +124 to 129
export const expectCaret = (start: number) => {
return ($el) => {
end = end == null ? start : end
// @ts-ignore
const end = start

expect(Cypress.dom.getSelectionBounds($el.get(0))).to.deep.eq({ start, end })
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This function is never called with a second argument in our code

_log?: Log
ensure?: object
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to internal types file

Copy link

cypress bot commented Feb 7, 2025

cypress    Run #60341

Run Properties:  status check passed Passed #60341  •  git commit eed2629a7b: Merge branch 'develop' into ts-to-js-driver-action-tests
Project cypress
Branch Review ts-to-js-driver-action-tests
Run status status check passed Passed #60341
Run duration 17m 53s
Commit git commit eed2629a7b: Merge branch 'develop' into ts-to-js-driver-action-tests
Committer Jennifer Shehane
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 6
Tests that did not run due to a developer annotating a test with .skip  Pending 1099
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 26542
View all changes introduced in this branch ↗︎
UI Coverage  45.56%
  Untested elements 191  
  Tested elements 164  
Accessibility  92.54%
  Failed rules  3 critical   8 serious   2 moderate   2 minor
  Failed elements 890  

@@ -2556,6 +2558,8 @@ declare namespace Cypress {
* expect(s).to.have.been.calledOnce
*/
withArgs(...args: any[]): Omit<A, 'withArgs'> & Agent<A>

callsFake(func: (...args: any[]) => any): Omit<A, 'withArgs'> & Agent<A>
Copy link
Member Author

Choose a reason for hiding this comment

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

There's a lot of methods that aren't extended from Sinon here. I added this one because our code was erroring trying to chain .as() off of cy.stub().callsFake()

@@ -3695,7 +3693,7 @@ declare namespace Cypress {
*
* @default 0
*/
duration: number
duration: number | string
Copy link
Member Author

Choose a reason for hiding this comment

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

string works actually, we convert it

@@ -3986,6 +3984,11 @@ declare namespace Cypress {
decode: boolean
}

/** Options to change the default behavior of .readFile */
interface ReadFileOptions extends Loggable {
encoding: Encodings
Copy link
Member Author

Choose a reason for hiding this comment

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

This option is valid and we handle it, not sure why its not documented.

@@ -557,7 +568,9 @@ describe('src/cy/commands/actions/type - #type special chars', () => {
cy.get('[contenteditable]:first').invoke('text', 'ab')
.type('{moveToStart}')
.then(($el) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

what type winds up coming through here, HTMLElement? Wonder if typing here prevents the unknown?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yah, just an HTMLElement, but also we don't know! so you have to convert to unknown first. In order to call .getSelection, it HAS to know that it's an HTMLInputElement. I almost feel like our own cypress types could be better about the type that it's passing through chaining that could help with this, but that sounds complex.

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.

2 participants