Skip to content

Incorrect usage of .contains() in installing-cypress-and-writing-your-first-test part #363

@helenanull

Description

@helenanull

In the very first test part, code:
cy.get("h1").contains("Testing Next.js Applications with Cypress")
is used and it is used incorrectly because previous text states:
Now that we know Cypress has the correct element, we need to write an assertion that the text contained within it is correct.

But .contains() is not an assertion at all, it gets a dom element.
In this case, we should be using .should('contain', "Testing Next.js Applications with Cypress") in there and in all the following cases (also even in the video)

It can be confusing if contains() command fails, because we get the error message "element not found" instead of actual difference in text.
Also, in the test runner, we'd get an assertion label:

Image

Maybe I'm missing something but wouldn't it be a better practise to use should('contain') instad of .contains() when we want to assert the text?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions