Skip to content

Commit 8f8bb60

Browse files
fix: Update cy.press signature to be Chainable (#31698)
* Update cy.press signature to be Chainable Since it's added via `Commands.add`, I believe `cy.press` should have a return type of `Chainable<null>` rather than `void` * Update first_spec.cy.js Updated test for chainable * use addAll rather than add to avoid type check * fix unit test * Add cli entry * update changelog --------- Co-authored-by: Jennifer Shehane <[email protected]> Co-authored-by: Jennifer Shehane <[email protected]>
1 parent 2b44398 commit 8f8bb60

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

cli/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ _Released 5/20/2025 (PENDING)_
99

1010
**Bugfixes:**
1111

12-
- Fixed an issue with the experimental usage of WebKit where Cypress incorrectly displayed `0` as the WebKit version. Addresses [#31684](https://github.com/cypress-io/cypress/issues/31684).
1312
- Fixed an issue where framebusting was occurring when `top.window.location` was being set explicitly. This fix does not require the `experimentalModifyObstructiveThirdPartyCode` configuration option. Addresses [#31687](https://github.com/cypress-io/cypress/issues/31687).
13+
- `cy.press()` now has a return type of `Chainable<null>` instead of `void` to match the convention of other commands that yield `null`. Addressed in [#31698](https://github.com/cypress-io/cypress/pull/31698).
14+
- Fixed an issue with the experimental usage of WebKit where Cypress incorrectly displayed `0` as the WebKit version. Addresses [#31684](https://github.com/cypress-io/cypress/issues/31684).
1415

1516
**Misc:**
1617

cli/types/cypress.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ declare namespace Cypress {
17551755
* cy.press(Cypress.Keyboard.Keys.TAB) // dispatches a keydown and press event to the browser, followed by a keyup event.
17561756
* @see https://on.cypress.io/press
17571757
*/
1758-
press(key: typeof Cypress.Keyboard.Keys[keyof typeof Cypress.Keyboard.Keys], options?: Partial<Loggable & Timeoutable>): void
1758+
press(key: typeof Cypress.Keyboard.Keys[keyof typeof Cypress.Keyboard.Keys], options?: Partial<Loggable & Timeoutable>): Chainable<null>
17591759

17601760
/**
17611761
* Get the immediately preceding sibling of each element in a set of the elements.

packages/driver/src/cy/commands/actions/press.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function (Commands: Cypress.Commands, Cypress: Cypress.Cypress, c
3636

3737
// throwErrByPath always throws, but there's no way to indicate that
3838
// code beyond this point is unreachable to typescript / linters
39-
return
39+
return null
4040
}
4141

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

50-
return
50+
return null
5151
}
5252

5353
if (Cypress.browser.name === 'firefox' && Number(Cypress.browser.majorVersion) < 135) {
@@ -71,7 +71,11 @@ export default function (Commands: Cypress.Commands, Cypress: Cypress.Cypress, c
7171
} catch (err) {
7272
$errUtils.throwErr(err, { onFail: log })
7373
}
74+
75+
return null
7476
}
7577

76-
return Commands.add('press', pressCommand)
78+
return Commands.addAll({
79+
press: pressCommand,
80+
})
7781
}

packages/driver/test/unit/cy/commands/actions/press.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ describe('cy/commands/actions/press', () => {
5151
},
5252
}
5353

54-
// @ts-expect-error - this is a generic mock impl
5554
Commands = {
56-
add: vi.fn(),
55+
// @ts-expect-error - this is a generic mock impl
56+
addAll: vi.fn(),
5757
}
5858

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

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

87-
expect(Commands.add).toHaveBeenCalledOnce()
87+
expect(Commands.addAll).toHaveBeenCalledOnce()
8888

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

92-
expect(cmdName).toEqual('press')
92+
expect(typeof obj.press).toBe('function')
9393

94-
press = cmd as PressCommand
94+
press = obj.press as PressCommand
9595
})
9696

9797
describe('with a valid key', () => {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
describe('this one should pass', () => {
22
it('dispatches a key press', () => {
3-
cy.press(Cypress.Keyboard.Keys.TAB)
3+
cy.press(Cypress.Keyboard.Keys.TAB).then((val) => {
4+
expect(val).to.be.null
5+
})
46
})
57
})

0 commit comments

Comments
 (0)