cypress-io/cypress-documentation

API `cy.end` example fails to run

MikeMcC399 opened this issue · 6 comments

Subject

API > Other Commands > end > Examples

Description

Current behavior

The following API example for cy.end() as shown on the documentation site

cy.contains('User: Cheryl')
  .click()
  .end() // yields null
  .contains('User: Charles')
  .click() // contains looks for content in document now

fails to run. The error is:

The subject received was:

> null

Desired behavior

API > Other Commands > end > Examples should provide an example of using cy.end() which runs.

Test code to reproduce

git clone --branch cypress-end https://github.com/MikeMcC399/cypress-test-tiny
cd cypress-test-tiny
npm ci
npx cypress run

Cypress Version

First reported on Cypress version: 13.7.0
Also reproducible on Cypress 13.15.2

Node version

v20.11.1 v20.17.0

Operating System

Ubuntu 22.04.4 LTS

Debug Logs

npx cypress run

DevTools listening on ws://127.0.0.1:39575/devtools/browser/3ea597d9-3fde-48a0-89a0-017c62cd3205

====================================================================================================

  (Run Starting)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Cypress:        13.7.0                                                                         │
  │ Browser:        Electron 118 (headless)                                                        │
  │ Node Version:   v20.11.1 (/home/mike/n/bin/node)                                               │
  │ Specs:          1 found (end.cy.js)                                                            │
  │ Searched:       cypress/e2e/**/*.cy.{js,jsx,ts,tsx}                                            │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


────────────────────────────────────────────────────────────────────────────────────────────────────

  Running:  end.cy.js                                                                       (1 of 1)


  End
    1) .end() - according to docs site


  0 passing (19s)
  1 failing

  1) End
       .end() - according to docs site:
     CypressError: Timed out retrying after 4000ms: `cy.contains()` failed because it requires a DOM element, window or document.

The subject received was:

  > `null`

The previous command that ran was:

  > `cy.end()`
      at isElement (https://example.cypress.io/__cypress/runner/cypress_runner.js:145288:58)
      at validateType (https://example.cypress.io/__cypress/runner/cypress_runner.js:145137:14)
      at Object.isType (https://example.cypress.io/__cypress/runner/cypress_runner.js:145168:7)
      at <unknown> (https://example.cypress.io/__cypress/runner/cypress_runner.js:132349:22)
      at Object.subjectFn (https://example.cypress.io/__cypress/runner/cypress_runner.js:144150:16)
      at $Cy.verifyUpcomingAssertions (https://example.cypress.io/__cypress/runner/cypress_runner.js:143494:31)
      at onRetry (https://example.cypress.io/__cypress/runner/cypress_runner.js:144141:15)
      at tryCatcher (https://example.cypress.io/__cypress/runner/cypress_runner.js:1807:23)
      at Promise.attempt.Promise.try (https://example.cypress.io/__cypress/runner/cypress_runner.js:4315:29)
      at whenStable (https://example.cypress.io/__cypress/runner/cypress_runner.js:144028:68)
      at <unknown> (https://example.cypress.io/__cypress/runner/cypress_runner.js:143969:14)
      at tryCatcher (https://example.cypress.io/__cypress/runner/cypress_runner.js:1807:23)
      at Promise._settlePromiseFromHandler (https://example.cypress.io/__cypress/runner/cypress_runner.js:1519:31)
      at Promise._settlePromise (https://example.cypress.io/__cypress/runner/cypress_runner.js:1576:18)
      at Promise._settlePromise0 (https://example.cypress.io/__cypress/runner/cypress_runner.js:1621:10)
      at Promise._settlePromises (https://example.cypress.io/__cypress/runner/cypress_runner.js:1701:18)
      at Promise._fulfill (https://example.cypress.io/__cypress/runner/cypress_runner.js:1645:18)
      at <unknown> (https://example.cypress.io/__cypress/runner/cypress_runner.js:5450:46)
  From Your Spec Code:
      at Context.eval (webpack://cypress-test-tiny/./cypress/e2e/end.cy.js:18:9)




  (Results)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Tests:        1                                                                                │
  │ Passing:      0                                                                                │
  │ Failing:      1                                                                                │
  │ Pending:      0                                                                                │
  │ Skipped:      0                                                                                │
  │ Screenshots:  1                                                                                │
  │ Video:        false                                                                            │
  │ Duration:     19 seconds                                                                       │
  │ Spec Ran:     end.cy.js                                                                        │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


  (Screenshots)

  -  /home/mike/github/cypress-io/cypress-test-tiny/cypress/screenshots/end.cy.js/End     (1280x720)
      -- .end() - according to docs site (failed).png


====================================================================================================

  (Run Finished)


       Spec                                              Tests  Passing  Failing  Pending  Skipped
  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ ✖  end.cy.js                                00:19        1        -        1        -        - │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘
    ✖  1 of 1 failed (100%)                     00:19        1        -        1        -        -

Other

Related issues

@MikeMcC399 Yah it's a weird little command. I don't totally understand why this would fail though. I wonder if this started in a more recent version like 12.0

@jennifer-shehane

Yah it's a weird little command. I don't totally understand why this would fail though. I wonder if this started in a more recent version like 12.0

I tested with earlier versions as well and all the ones I tested showed failure:

Node.js v20.11.1

Cypress version result
10.11.0 failed
11.2.0 failed
12.7.4 failed
13.7.0 failed

Bottom line is that any example for cy.end() should work and should pass linting.

I don't have enough experience writing Cypress tests to be able to specify a good replacement example for the use of cy.end(), however I did find it used with Cypress.log:

e.g. log.end() in cypress-realworld-testing:

See https://learn.cypress.io/real-world-examples/custom-cypress-commands

Perhaps this could be used as an alternative?

I think we should just remove the cy.end example. The kitchensink isn't exhaustive anyway and there's less utility for this command with the changes we've made to chaining.

@jennifer-shehane

Would you want to remove the examples from the documentation section API > Other Commands > end > Examples ?

Thank you for explaining about the history. I didn't know about that.

image

If they're not working in these examples, we can remove them or come up with some example that does make sense to add. Something that's not chaining off of action commands I guess.