Skip to content

[solved] I get an error message if I don't prepend calls to findBy... with a get('body') call #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dutzi opened this issue Jun 23, 2020 · 6 comments

Comments

@dutzi
Copy link

dutzi commented Jun 23, 2020

  • cypress-testing-library version: ^6.0.0
  • node version: v10.21.0
  • npm (or yarn) version: 1.22.4 (yarn)

Relevant code or config

    cy.visit('/email-verified')
      .findByText(/okay/i)

What you did:

I visit a page and call findByText

What happened:

I get the following error message:

Timed out retrying: Expected container to be an Element, a Document or a DocumentFragment but got Window.

image

If I call get('body') before calling findByText it works:

    cy.visit('/email-verified')
      .get('body')
      .findByText(/okay/i)
@dutzi
Copy link
Author

dutzi commented Jun 23, 2020

Just posted this for anyone encoutering this. Turns out cypress-testing-library no longer supports chaining commands, so you have to write something like:

cy.visit('/page');
cy.findByText(/okay/i).click();
cy.findByText(/another button/i).click()

@dutzi dutzi closed this as completed Jun 23, 2020
@selbekk
Copy link

selbekk commented Jul 1, 2020

This sounds like a bug?

@kentcdodds
Copy link
Member

Hmmm... cc @NicholasBoll?

@selbekk
Copy link

selbekk commented Jul 1, 2020

Oh sorry, it’s just a new API - you’re not supposed to chain stuff any more. Perhaps we could implement a better error message?

@NicholasBoll
Copy link
Contributor

@dutzi It isn't true that Cypress Testing Library no longer supports chaining. It now supports scoping. All Cypress Testing Library queries are called Dual Commands: https://docs.cypress.io/api/cypress-api/custom-commands.html#Dual-Commands.

Previously they were Parent Commands: https://docs.cypress.io/api/cypress-api/custom-commands.html#Parent-Commands

The difference is if a query starts a chain, it will assume a global context (find from the root of the document). If you have a previous subject, it will start there.

Before it wasn't possible to scope any find* queries. Now you can. I've had conversations with the core Cypress team and they recommend starting a new chain when using Parent commands like cy.get. I couldn't find specific recommendation against the pattern of chaining .get from a previous chain, but none of the documentation chains a .get.

I like to think of a "chain" like I think of a sentence. There is a primary subject and something acting on that subject - either an action (like click) or an assertion (like should). Subjects can be narrowed (like adjectives to help describe the subject) and related actions can be grouped. You might say something like "We went to the fair and enjoyed ourselves". That's a compound sentence with a related subject. Where did you enjoy yourselves? At the fair. Who enjoyed themselves? We did. The same is true for Cypress command chains. It is easier to understand when related groups of commands are part of a chain. A 20+ chain of commands doesn't have any central theme or breaking point. Everything runs together like a run-on sentence without structure.

More info on subject management: https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Subject-Management

Change was here: #130

@kentcdodds
Copy link
Member

Very well said. Thanks @NicholasBoll 👍👍👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants