-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Documentation for Cypress.ensure #5054
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
390a0d0
Documentation for Cypress.ensure
BlueWinds 6b67ce7
Apply suggestions from code review
BlueWinds 259aa06
Apply suggestions from code review
BlueWinds 382a578
Review updates
BlueWinds 6c3f3d3
Prettier run
BlueWinds ee0c5f7
Merge branch 'main' into issue-4881-cypress-ensure-docs
BlueWinds 519945f
Remove docs around ensure.isChildCommand since we intend to remove it.
BlueWinds e8fb044
Prettier run
BlueWinds a6297fe
Merge branch 'main' into issue-4881-cypress-ensure-docs
emilyrohrbough File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
title: Cypress.ensure | ||
--- | ||
|
||
`Cypress.ensure` is a collection of helper methods for making assertions. They | ||
are mostly useful when writing [custom queries](/api/cypress-api/custom-queries) | ||
or [custom commands](/api/cypress-api/custom-commands). | ||
|
||
Most functions on `Cypress.ensure` accept a | ||
[`subject`](/guides/core-concepts/introduction-to-cypress#Subject-Management) | ||
argument, check an assertion, and throw an error if the assertion fails. These | ||
functions have no return value. | ||
|
||
## Syntax | ||
|
||
```javascript | ||
// Type of argument | ||
Cypress.ensure.isType(subject, type, commandName, cy) | ||
Cypress.ensure.isElement(subject, commandName, cy) | ||
Cypress.ensure.isWindow(subject, commandName, cy) | ||
Cypress.ensure.isDocument(subject, commandName, cy) | ||
|
||
// State of DOM element | ||
Cypress.ensure.isAttached(subject, commandName, cy) | ||
Cypress.ensure.isNotDisabled(subject, commandName) | ||
Cypress.ensure.isNotHiddenByAncestors(subject, commandName) | ||
Cypress.ensure.isNotReadonly(subject, commandName) | ||
Cypress.ensure.isScrollable(subject, commandName) | ||
Cypress.ensure.isStrictlyVisible(subject, commandName) | ||
Cypress.ensure.isVisible(subject, commandName) | ||
``` | ||
|
||
:::caution | ||
|
||
Many of these functions accept an optional `onFail` argument. This is a legacy | ||
feature used to customize the thrown error, and may be removed in a future | ||
release; we recommend against relying on it. If you need more control over the | ||
error thrown, write your own `ensure` function instead. | ||
|
||
::: | ||
|
||
### Usage | ||
|
||
**<Icon name="check-circle" color="green" /> Correct Usage** | ||
|
||
```javascript | ||
Cypress.Commands.addQuery('getChildById', function (id) { | ||
return (subject) => { | ||
// Verify that the subject is an element, document, or window object | ||
Cypress.ensure.isType( | ||
subject, | ||
['element', 'document', 'window'], | ||
'getChildById', | ||
cy | ||
) | ||
|
||
return $$(`#${id}`, subject) | ||
} | ||
}) | ||
|
||
BlueWinds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const queryName = 'verifyElementActionable' | ||
|
||
Cypress.Commands.addQuery(queryName, function (...args) { | ||
return (subject) => { | ||
// Verify that the subject fulfills a variety of conditions | ||
Cypress.ensure.isElement(subject, queryName, cy) | ||
Cypress.ensure.isVisible(subject, queryName, cy) | ||
Cypress.ensure.isNotDisabled(subject, queryName, cy) | ||
Cypress.ensure.isNotReadonly(subject, queryName, cy) | ||
|
||
return subject | ||
} | ||
}) | ||
``` | ||
|
||
## See also | ||
|
||
- ["Custom Queries"](/api/cypress-api/custom-queries) contains more information | ||
about writing custom queries, which is the main use-case for the `ensure` | ||
functions. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should just not document this, since the user can just
try...catch
to get the same functionality asonFail
, right? Then we can just rip it out when we're done using it internally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I gave a similar suggestion to not document this #5048 (comment). The onFail ties in to how this would be removed up if this callback matched the command onFail behavior, so it's require quite a bit of user-knowledge to handle this correctly. I like @flotwig's suggestion for handling with try-catch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was the response I gave to Emily, and I still feel the same way - if it exists, we should not leave it silently undocumented. I've found myself in this boat several times dealing with other projects, where I see an argument is there, but the documentation is completely silent. What does it do? Why is it there? Your own code uses it, to what purpose?
I think a warning "it exists, but don't use it" is appropriate, as well as not including it in the documented function signatures.