question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cypress commands (e.g. cy.get) return undefined if previous test step fails

See original GitHub issue

Current behavior:

Cypress test runner fails in after() block with error:

CypressError: cy.first() failed because it requires a DOM element.

The subject received was:

  > undefined

The previous command that ran was:

  > cy.get()

Because this error occurred during a 'after all' hook we are skipping the remaining tests in the current suite:

This happens each time a test step fails. In this case, it happens when the following assertion fails

.get('div.header__dob').should('contain', `${patient.birth_date}`)

Desired behavior:

Cleanup step should execute with Cypress commands returning values instead of undefined

Steps to reproduce: (app code and test code)

  1. Write test with before, test & after steps
  2. Have an assertion fail during testing
  3. cy.get should return undefined in after step

Versions

Cypress: 3.2.0 OS: MacOS 10.14.4 Browser: Electron59 & Chrome73

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mmonteiroccommented, Mar 23, 2021

Hi ! I’m having the same issue when doing cy.visit() to a new url.

We have a custom command which we use to check if our loading dissappear.

When arriving to that URL I have a method which waits for our loading to dissappear (cy.get(loading).should(‘not.exist’)…

Before doing that, we check that the main element in our DOM exists…

The issue comes in that check when in this custom command, we do cy.get(‘mainElement’, { timeout: 10000 }).should(‘exist’); We only do that assertion and afterwards assert that the loading does not exists

I get the following: For some reasons, I cannot show you the ID we use. anyways tried with other ID’s / css selectors and same error so that’s not an issue image

0reactions
Deopcommented, Oct 28, 2022

Is there any update on this issue? Having the same problem with undefined elements in after methods after test fail

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Messages | Cypress Documentation
We found an error preparing your test file. This message means that Cypress encountered an error when compiling and/or bundling your test file....
Read more >
Retry-ability - Cypress Documentation
If the assertion still fails, Cypress continues retrying until the cy.find() timeout is reached. Retry-ability allows the test to complete each query as...
Read more >
then | Cypress Documentation
If the callback returns undefined or null (or there is no return value) and the callback does not call any Cypress commands, the...
Read more >
Custom Commands - Cypress Documentation
js file, since it is loaded before any test files are evaluated via an import statement in the supportFile. Syntax. Cypress.Commands.add(name ...
Read more >
Introduction to Cypress - Cypress Documentation
Cypress halts and fails the test. .get('#element-does-not-exist') // ...this ... If chained directly off of cy , or if the previous command yielded...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found