{
  "doc": {
    "id": "api/cypress-api/ensure",
    "title": "Cypress.ensure",
    "description": "Cypress.ensure is a collection of helper methods for making assertions. They are mostly useful when writing custom queries or custom commands.",
    "section": "api",
    "source_path": "/llm/markdown/api/cypress-api/ensure.md",
    "version": "29f95bf8bb06f320986f3749f5bf09a35a409eab",
    "updated_at": "2026-09-04T10:49:54.630Z",
    "headings": [
      {
        "id": "api/cypress-api/ensure#cypress-ensure",
        "text": "Cypress.ensure",
        "level": 1
      },
      {
        "id": "api/cypress-api/ensure#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/cypress-api/ensure#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/cypress-api/ensure#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "Cypress.ensure"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`Cypress.ensure` is a collection of helper methods for making assertions. They are mostly useful when writing "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/cypress-api/custom-queries.md",
            "children": [
              {
                "type": "text",
                "value": "custom queries"
              }
            ]
          },
          {
            "type": "text",
            "value": " or "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/cypress-api/custom-commands.md",
            "children": [
              {
                "type": "text",
                "value": "custom commands"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Most functions on `Cypress.ensure` accept a "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management",
            "children": [
              {
                "type": "text",
                "value": "`subject`"
              }
            ]
          },
          {
            "type": "text",
            "value": " argument, check an assertion, and throw an error if the assertion fails. These functions have no return value."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Syntax"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// Type of argument\nCypress.ensure.isType(subject, type, commandName, cy)​\nCypress.ensure.isElement(subject, commandName, cy)​\nCypress.ensure.isWindow(subject, commandName, cy)\nCypress.ensure.isDocument(subject, commandName, cy)​\n\n// State of DOM element\nCypress.ensure.isAttached(subject, commandName, cy)​\nCypress.ensure.isNotDisabled(subject, commandName)​\nCypress.ensure.isNotHiddenByAncestors(subject, commandName)​\nCypress.ensure.isNotReadonly(subject, commandName)​\nCypress.ensure.isScrollable(subject, commandName)​\nCypress.ensure.isStrictlyVisible(subject, commandName)​\nCypress.ensure.isVisible(subject, commandName)​"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "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."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Usage"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Correct Usage"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress/support/commands.js"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress/support/commands.ts"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "Cypress.Commands.addQuery('getChildById', function (id) {\n  return (subject) => {\n    // Verify that the subject is an element, document, or window object\n    Cypress.ensure.isType(\n      subject,\n      ['element', 'document', 'window'],\n      'getChildById',\n      cy\n    )\n\n    return $$(`#${id}`, subject)\n  }\n})\n\nconst queryName = 'verifyElementActionable'\n\nCypress.Commands.addQuery(queryName, function (...args) {\n  return (subject) => {\n    // Verify that the subject fulfills a variety of conditions\n    Cypress.ensure.isElement(subject, queryName, cy)\n    Cypress.ensure.isVisible(subject, queryName, cy)\n    Cypress.ensure.isNotDisabled(subject, queryName, cy)\n    Cypress.ensure.isNotReadonly(subject, queryName, cy)\n\n    return subject\n  }\n})"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "declare global {\n  namespace Cypress {\n    interface Chainable {\n      getChildById(id: string): Chainable<JQuery>\n      verifyElementActionable(...args: any[]): Chainable<JQuery>\n    }\n  }\n}\n\nCypress.Commands.addQuery('getChildById', function (id) {\n  return (subject) => {\n    // Verify that the subject is an element, document, or window object\n    Cypress.ensure.isType(\n      subject,\n      ['element', 'document', 'window'],\n      'getChildById',\n      cy\n    )\n\n    return $$(`#${id}`, subject)\n  }\n})\n\nconst queryName = 'verifyElementActionable'\n\nCypress.Commands.addQuery(queryName, function (...args) {\n  return (subject) => {\n    // Verify that the subject fulfills a variety of conditions\n    Cypress.ensure.isElement(subject, queryName, cy)\n    Cypress.ensure.isVisible(subject, queryName, cy)\n    Cypress.ensure.isNotDisabled(subject, queryName, cy)\n    Cypress.ensure.isNotReadonly(subject, queryName, cy)\n\n    return subject\n  }\n})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "See also"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/cypress-api/custom-queries.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "\"Custom Queries\""
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": " contains more information about writing custom queries, which is the main use-case for the `ensure` functions."
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "token_estimate": 443
}