Skip to content

Unhelpful error messages for query* commands #103

Closed
@NicholasBoll

Description

@NicholasBoll
  • cypress-testing-library version: 5.1.0

Relevant code or config

Test

cy.queryByLabelText('Test 1').click()
cy.queryByLabelText('Test 2').click()
cy.queryByLabelText('Test 3').click()
cy.queryByLabelText('Test 4').click()

HTML

Passing - no issues
<label for="1">Test 1</label>
<input id="1" />

Failing - couldn't find the label element
<label for="2">Test Oops</label>
<input id="2">

Failing - there is no `for` attribute on the label element
<label>Test 3</label>
<input id="3" />

Failing - couldn't find the input element
<label for="4">Test 4</label>
<input />

What happened:

The first passes fine. The consoleProps do not have the same description level as built-in Cypress commands

The second fails, but not as expected. The issue is the label element could not be found by the text "Test 2"

The third fails, but not as expected. The issue is the label element doesn't have a for attribute and has no way to associate with an input

The fourth fails, but not as expected. The issue is the label element has no corresponding input element.

All failures result in this error message:

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

The subject received was:

  > {}

The previous command that ran was:

  > cy.then()

Error image described by code block

Note that the failure is on the cy.click command and not the queryByLabelText command. This is because https://github.com/testing-library/cypress-testing-library/blob/master/src/index.js#L87 simply wraps any returned value in an jQuery wrapper. It seems to be returning an empty object rather than an empty NodeList which Cypress automatically detects.

Also notice the error mentions the previous command was cy.then, even though that command wasn't actually used anywhere in user code. This is confusing. This is because of https://github.com/testing-library/cypress-testing-library/blob/master/src/index.js#L84 (use of cy.window().then()

There is also a comment about Promises not working with Cypress retryability which is not accurate. The problem probably comes from the use of cy.then. The original intent of Cypress Custom commands was not to use any cy commands at all. Internal Cypress Commands do not use them. Here's the source code for all the traversal-style commands: https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/traversals.coffee. Notice promises are used in there just fine.

Problem description:
Failures to find elements returned from @testing-library/dom are unhelpful and confusing

Suggested solution:
Refactor the use of Cypress Custom Commands a bit to better handle not finding an element and remove the use of cy.window() and cy.then. Cypress has a cy.state function and it saves the window in cy.state('window') which is synchronously resolved.

Also try some of the query commands with known failures and see if the errors are useful. This goes a long way to guiding developers to finding out why something went wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions