diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index fb76a32bfc..366c2b4aa1 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -9,8 +9,9 @@ _Released 5/20/2025 (PENDING)_ **Bugfixes:** -- 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). - 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). +- `cy.press()` now has a return type of `Chainable` instead of `void` to match the convention of other commands that yield `null`. Addressed in [#31698](https://github.com/cypress-io/cypress/pull/31698). +- 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). **Misc:** diff --git a/cli/types/cypress.d.ts b/cli/types/cypress.d.ts index 0104b365cc..65f51f9672 100644 --- a/cli/types/cypress.d.ts +++ b/cli/types/cypress.d.ts @@ -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): void + press(key: typeof Cypress.Keyboard.Keys[keyof typeof Cypress.Keyboard.Keys], options?: Partial): Chainable /** * Get the immediately preceding sibling of each element in a set of the elements. diff --git a/packages/driver/src/cy/commands/actions/press.ts b/packages/driver/src/cy/commands/actions/press.ts index 5e96fa5032..7c4110f35d 100644 --- a/packages/driver/src/cy/commands/actions/press.ts +++ b/packages/driver/src/cy/commands/actions/press.ts @@ -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') { @@ -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) { @@ -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, + }) } diff --git a/packages/driver/test/unit/cy/commands/actions/press.spec.ts b/packages/driver/test/unit/cy/commands/actions/press.spec.ts index 2e77adae79..228afe24d7 100644 --- a/packages/driver/test/unit/cy/commands/actions/press.spec.ts +++ b/packages/driver/test/unit/cy/commands/actions/press.spec.ts @@ -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 @@ -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', () => { diff --git a/system-tests/projects/cy-press-second-spec-error/cypress/e2e/first_spec.cy.js b/system-tests/projects/cy-press-second-spec-error/cypress/e2e/first_spec.cy.js index 6c63f4b145..0c8f111882 100644 --- a/system-tests/projects/cy-press-second-spec-error/cypress/e2e/first_spec.cy.js +++ b/system-tests/projects/cy-press-second-spec-error/cypress/e2e/first_spec.cy.js @@ -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 + }) }) })