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
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _Released 5/20/2025 (PENDING)_
**Misc:**

- Cursor is now available as an IDE option for opening files in Cypress, if it is installed on your system. Addressed in [#31691](https://github.com/cypress-io/cypress/pull/31691).
- `cy.press()` types now have a return type of `Chainable<null>` instead of `void`. Addressed in [#31698](https://github.com/cypress-io/cypress/pull/31698).

**Dependency Updates:**

Expand Down
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,
})
}
12 changes: 6 additions & 6 deletions packages/driver/test/unit/cy/commands/actions/press.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ describe('cy/commands/actions/press', () => {
},
}

// @ts-expect-error - this is a generic mock impl
Commands = {
add: vi.fn(),
// @ts-expect-error - this is a generic mock impl
addAll: vi.fn(),
}

// @ts-expect-error
Expand Down Expand Up @@ -84,14 +84,14 @@ describe('cy/commands/actions/press', () => {

addCommand(Commands, Cypress, cy, state, config)

expect(Commands.add).toHaveBeenCalledOnce()
expect(Commands.addAll).toHaveBeenCalledOnce()

// @ts-expect-error
const [[cmdName, cmd]]: [[string, PressCommand]] = Commands.add.mock.calls
const [[obj]]: [[{press: PressCommand}]] = Commands.addAll.mock.calls

expect(cmdName).toEqual('press')
expect(typeof obj.press).toBe('function')

press = cmd as PressCommand
press = obj.press as PressCommand
})

describe('with a valid key', () => {
Expand Down
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