Skip to content

fix: Update cy.press signature to be Chainable #31698

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
May 14, 2025
2 changes: 1 addition & 1 deletion cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,7 @@ declare namespace Cypress {
* cy.press(Cypress.Keyboard.Keys.TAB) // dispatches a keydown and press event to the browser, followed by a keyup event.
* @see https://on.cypress.io/press
*/
press(key: typeof Cypress.Keyboard.Keys[keyof typeof Cypress.Keyboard.Keys], options?: Partial<Loggable & Timeoutable>): void
press(key: typeof Cypress.Keyboard.Keys[keyof typeof Cypress.Keyboard.Keys], options?: Partial<Loggable & Timeoutable>): Chainable<null>

/**
* Get the immediately preceding sibling of each element in a set of the elements.
Expand Down
10 changes: 7 additions & 3 deletions packages/driver/src/cy/commands/actions/press.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function (Commands: Cypress.Commands, Cypress: Cypress.Cypress, c

// throwErrByPath always throws, but there's no way to indicate that
// code beyond this point is unreachable to typescript / linters
return
return null
}

if (Cypress.browser.family === 'webkit') {
Expand All @@ -47,7 +47,7 @@ export default function (Commands: Cypress.Commands, Cypress: Cypress.Cypress, c
},
})

return
return null
}

if (Cypress.browser.name === 'firefox' && Number(Cypress.browser.majorVersion) < 135) {
Expand All @@ -71,7 +71,11 @@ export default function (Commands: Cypress.Commands, Cypress: Cypress.Cypress, c
} catch (err) {
$errUtils.throwErr(err, { onFail: log })
}

return null
}

return Commands.add('press', pressCommand)
return Commands.addAll({
press: pressCommand,
})
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
describe('this one should pass', () => {
it('dispatches a key press', () => {
cy.press(Cypress.Keyboard.Keys.TAB)
cy.press(Cypress.Keyboard.Keys.TAB).then((val) => {
expect(val).to.be.null
})
})
})
Loading