Skip to content

Commit 1e7b596

Browse files
misc: Add some missing types + Convert js driver/query tests to ts (#31154)
* misc: Add some missing types + Convert js driver/query tests to ts * changelog entry * reuse partial * Update cli/CHANGELOG.md * Update call to querySelector to assign an identifier when checking for null
1 parent 5da0995 commit 1e7b596

File tree

11 files changed

+127
-106
lines changed

11 files changed

+127
-106
lines changed

cli/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _Released 2/25/2025 (PENDING)_
1111

1212
- Viewport width, height, and scale now display in a badge above the application under test. The dropdown describing how to set viewport height and width has been removed from the UI. Additionally, component tests now show a notice about URL navigation being disabled in component tests. Addresses [#30999](https://github.com/cypress-io/cypress/issues/30999). Addressed in [#31119](https://github.com/cypress-io/cypress/pull/31119).
1313
- Updated types around `.readFile()` and `.scrollTo()` arguments and `Cypress.dom` methods. Addressed in [#31055](https://github.com/cypress-io/cypress/pull/31055).
14+
- Updated types around `.shadow()` and `.root()` options. Addressed in [#31154](https://github.com/cypress-io/cypress/pull/31154).
1415

1516
**Dependency Updates:**
1617

cli/types/cypress.d.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,7 @@ declare namespace Cypress {
18981898
*
18991899
* @see https://on.cypress.io/root
19001900
*/
1901-
root<E extends Node = HTMLHtmlElement>(options?: Partial<Loggable>): Chainable<JQuery<E>> // can't do better typing unless we ignore the `.within()` case
1901+
root<E extends Node = HTMLHtmlElement>(options?: Partial<LogTimeoutOptions>): Chainable<JQuery<E>> // can't do better typing unless we ignore the `.within()` case
19021902

19031903
/**
19041904
* Take a screenshot of the application under test and the Cypress Command Log.
@@ -1980,6 +1980,18 @@ declare namespace Cypress {
19801980
*/
19811981
shadow(): Chainable<Subject>
19821982

1983+
/**
1984+
* Traverse into an element's shadow root.
1985+
*
1986+
* @example
1987+
* cy.get('my-component')
1988+
* .shadow({ timeout: 10000, log: false })
1989+
* .find('.my-button')
1990+
* .click()
1991+
* @see https://on.cypress.io/shadow
1992+
*/
1993+
shadow(options?: Partial<LogTimeoutOptions>): Chainable<Subject>
1994+
19831995
/**
19841996
* Create an assertion. Assertions are automatically retried until they pass or time out.
19851997
*
@@ -2732,6 +2744,7 @@ declare namespace Cypress {
27322744

27332745
interface CheckClearOptions extends Loggable, Timeoutable, ActionableOptions { }
27342746

2747+
interface LogTimeoutOptions extends Loggable, Timeoutable { }
27352748
/**
27362749
* Object to change the default behavior of .click().
27372750
*/

packages/driver/cypress/component/spec.cy.js packages/driver/cypress/component/spec.cy.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { sinon } = Cypress
22

33
describe('component testing', () => {
4-
/** @type {Cypress.Agent<sinon.SinonSpy>} */
54
let uncaughtExceptionStub
65

76
before(() => {
@@ -16,7 +15,11 @@ describe('component testing', () => {
1615

1716
beforeEach(() => {
1817
uncaughtExceptionStub.resetHistory()
19-
document.querySelector('[data-cy-root]').innerHTML = ''
18+
const root = document.querySelector('[data-cy-root]')
19+
20+
if (root) {
21+
root.innerHTML = ''
22+
}
2023
})
2124

2225
it('fails and shows an error', () => {
@@ -28,7 +31,7 @@ describe('component testing', () => {
2831
throw new Error('An error!')
2932
})
3033

31-
document.querySelector('[data-cy-root]').appendChild($el)
34+
document.querySelector('[data-cy-root]')?.appendChild($el)
3235
cy.get('button').click().then(() => {
3336
expect(uncaughtExceptionStub).to.have.been.calledOnceWithExactly(null)
3437
expect(Cypress.log).to.be.calledWithMatch(sinon.match({ 'message': `Error: An error!`, name: 'uncaught exception' }))
@@ -40,11 +43,12 @@ describe('component testing', () => {
4043
const $el = document.createElement('button')
4144

4245
$el.innerText = `Don't click it!`
46+
// @ts-expect-error - testing an error state
4347
$el.addEventListener('click', new Promise((_, reject) => {
4448
reject('Promise rejected with a string!')
4549
}))
4650

47-
document.querySelector('[data-cy-root]').appendChild($el)
51+
document.querySelector('[data-cy-root]')?.appendChild($el)
4852
cy.get('button').click().then(() => {
4953
expect(uncaughtExceptionStub).to.have.been.calledOnceWithExactly(null)
5054
expect(Cypress.log).to.be.calledWithMatch(sinon.match({ 'message': `Error: "Promise rejected with a string!"`, name: 'uncaught exception' }))

packages/driver/cypress/e2e/commands/querying/focused.cy.js packages/driver/cypress/e2e/commands/querying/focused.cy.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { assertLogLength } = require('../../../support/utils')
1+
import { assertLogLength } from '../../../support/utils'
22

33
const { _ } = Cypress
44

0 commit comments

Comments
 (0)