Skip to content

docs: Update Cypress Testing Library scoping example #373

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

Merged
merged 2 commits into from
Mar 3, 2020

Conversation

NicholasBoll
Copy link
Contributor

Recently testing-library/cypress-testing-library#108 added a way to take the previous subject of a previous command to scope the query. Also find* queries handle .should('not.exist') and should be preferred over query* which require additional logic for eventual non-existence queries.

Recently testing-library/cypress-testing-library#108 added a way to take the previous subject of a previous command to scope the query. Also `find*` queries handle `.should('not.exist')` and should be preferred over `query*` which require additional logic for eventual non-existence queries.
> need retryability and `find*` queries already support that. The reason the `query*`
> variants are supported is to allow you to assert that elements do _not_ appear on
> the screen.
> need retryability and `find*` queries already support that. `query*` queries are not
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

query* queries are no longer needed for asserting that elements do not appear in the DOM. A find* query can do that instead:

// Doesn't exist now
cy.findByText('Non-existing Button Text').should('not.exist')

// Eventually won't exist
cy.findByText('Eventually will not exist').should('not.exist')

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd include a note about why they are "not necessary", including version when the find* change was added, and mention plans for future deprecation here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with @alexkrolick here 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think find was changed in testing-library/cypress-testing-library#78 and released as 5.0.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the following sound?

https://github.com/testing-library/testing-library-docs/pull/373/files#diff-6245e2573aa625d927375e2065f25b1cR29-R32

Note: the get* queries are not supported because for reasonable Cypress tests you
need retryability and find* queries already support that. query* queries are no longer
necessary since v5 and will be removed in v6. find* fully support built-in Cypress
assertions (removes the only use-case for query*).

cy.queryByLabelText('Label text', {timeout: 7000}).should('exist')
cy.findByText('Button Text').should('exist')
cy.findByText('Non-existing Button Text').should('not.exist')
cy.findByLabelText('Label text', {timeout: 7000}).should('exist')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find* should be preferred over query*

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'm glad that we're keeping all of the query* stuff off here considering we're going to be removing it. Thanks!

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Just one small thing. Thanks!

> need retryability and `find*` queries already support that. The reason the `query*`
> variants are supported is to allow you to assert that elements do _not_ appear on
> the screen.
> need retryability and `find*` queries already support that. `query*` queries are not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with @alexkrolick here 👍

cy.queryByLabelText('Label text', {timeout: 7000}).should('exist')
cy.findByText('Button Text').should('exist')
cy.findByText('Non-existing Button Text').should('not.exist')
cy.findByLabelText('Label text', {timeout: 7000}).should('exist')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'm glad that we're keeping all of the query* stuff off here considering we're going to be removing it. Thanks!

@NicholasBoll
Copy link
Contributor Author

I'm not sure what the ci check failures are. They seem to be npm issues?

5:02:15 PM: WARN tar ENOENT: no such file or directory, open '/opt/build/repo/website/node_modules/.staging/rxjs-414f633c/add/observable/onErrorResumeNext.js.map'

@NicholasBoll NicholasBoll force-pushed the pr/update-cypress-docs branch from fb36ae8 to f69a8d5 Compare February 18, 2020 19:28
Comment on lines 62 to 64
cy.get('form').then(subject => {
cy.findByText('Button Text', {container: subject}).should('exist')
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do people think about removing these lines? It is still supported, but the above example is much cleaner and more natural to other Cypress code.

@kentcdodds kentcdodds merged commit 3448319 into master Mar 3, 2020
@kentcdodds kentcdodds deleted the pr/update-cypress-docs branch March 3, 2020 18:34
@kentcdodds
Copy link
Member

Awesome. Thanks!

@kentcdodds
Copy link
Member

@all-contributors please add @NicholasBoll for docs

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @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

Successfully merging this pull request may close these issues.

3 participants