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.

Chained cypress-testing-library commands don't work as expected

See original GitHub issue
  • cypress-testing-library version: 6.0.0
  • node version: 12.14.1
  • npm (or yarn) version: yarn 1.21.1

Relevant code or config

cy.findAllByTestId('test-id').findByText('mytext').parents('.parent');

What you did: I want to chain commands from cypress-testing-library, so that they are executed in the same context. In the example above I want to retrieve the element which contains the text “mytext” but only for elements which have a parent (somewhere in the upper DOM) with a data-testid attribute with the value ‘test-id’.

What happened: The command doesn’t find an element with the given text, even if there would be such an element (proof is being provided in the Hint below).

image

Hint:

If I run the same code as above, but with a contains instead of the findByText everything works as expected:

cy.findAllByTestId('test-id').contains('mytext').parents('.parent');

image

Thanks for this library and the whole testing-library family in general! Cheers!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
ismaycommented, Feb 24, 2021

It’s a bit unclear to me whether the chaining behaviour was removed on purpose (as I read @kentcdodds reply), or if it’s a bug (@NicholasBoll’s reply).

Personally, I think the chaining is useful. For example, say that in a table I want to verify that a particular cell (selected by the ‘rowheader’ role), has sibling cells with a certain expected content.

If you can’t chain I don’t see how I could gradually scope the selection of elements. For a practical example, see the screenshot below:

Screenshot 2021-02-24 at 11 45 53

Let’s say I want to ensure that Job 1 (rowheader) is rendering with the expected type ‘Credentials expiry alert’. I’d expect to be able to do:

    cy.findByRole('rowheader', { name: 'Job 1' })
        .siblings()
        .findByRole('cell', { name: 'Credentials expiry alert' })
        .should('exist')

Yes, I could do this (and that works):

    cy.findByRole('cell, { name: 'Credentials expiry alert' }).should('exist')

But that doesn’t guarantee that the expected cell is rendered in the row that contains the Job 1 rowheader.

3reactions
kentcdoddscommented, Apr 7, 2020

Hi @silviuaavram,

I’m guessing your issue is related to the breaking change documented here: https://github.com/testing-library/cypress-testing-library/releases/tag/v6.0.0

Should be a reasonably easy update for you 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Messages | Cypress Documentation
This message means you tried to execute one or more Cypress commands outside of a currently running test. Cypress has to be able...
Read more >
Cypress Testing Library
Cypress Testing Library extends Cypress's cy commands. ... You can now use all of DOM Testing Library 's findBy , findAllBy , queryBy...
Read more >
cypress-testing-library - Bountysource
Chained cypress -testing-library commands don't work as expected $ 0. Created 2 years ago in testing-library/cypress-testing-library with 9 comments. cypress- ...
Read more >
What is Cypress Testing Library and its Usage - BrowserStack
To query web elements, Cypress provides commands like get(), contains(), find() and within(). Using these commands, you further need to chain ...
Read more >
Cypress Testing Library Intro - YouTube
This is a basic intro to Cypress Testing Library commands that you could use in your tests. It provides a new way to...
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