{
  "doc": {
    "id": "api/commands/wrap",
    "title": "wrap | Cypress Documentation",
    "description": "Yield the object passed into .wrap() in Cypress",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/wrap.md",
    "version": "ce02913654e2655ee63448bdc92bb92c7b46a619",
    "updated_at": "2026-04-22T19:37:51.587Z",
    "headings": [
      {
        "id": "api/commands/wrap#wrap",
        "text": "wrap",
        "level": 1
      },
      {
        "id": "api/commands/wrap#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/wrap#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/wrap#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/wrap#yields-learn-about-subject-management",
        "text": "Yields Learn about subject management",
        "level": 3
      },
      {
        "id": "api/commands/wrap#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/wrap#objects",
        "text": "Objects",
        "level": 3
      },
      {
        "id": "api/commands/wrap#invoke-the-function-on-the-subject-in-wrap-and-return-the-new-value",
        "text": "Invoke the function on the subject in wrap and return the new value",
        "level": 4
      },
      {
        "id": "api/commands/wrap#elements",
        "text": "Elements",
        "level": 3
      },
      {
        "id": "api/commands/wrap#wrap-elements-to-continue-executing-commands",
        "text": "Wrap elements to continue executing commands",
        "level": 4
      },
      {
        "id": "api/commands/wrap#conditionally-wrap-elements",
        "text": "Conditionally wrap elements",
        "level": 4
      },
      {
        "id": "api/commands/wrap#promises",
        "text": "Promises",
        "level": 3
      },
      {
        "id": "api/commands/wrap#simple-example",
        "text": "Simple example",
        "level": 4
      },
      {
        "id": "api/commands/wrap#application-example",
        "text": "Application example ",
        "level": 4
      },
      {
        "id": "api/commands/wrap#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/wrap#requirements-learn-about-chaining-commands",
        "text": "Requirements Learn about chaining commands",
        "level": 3
      },
      {
        "id": "api/commands/wrap#assertions-learn-about-assertions",
        "text": "Assertions Learn about assertions",
        "level": 3
      },
      {
        "id": "api/commands/wrap#timeouts-learn-about-timeouts",
        "text": "Timeouts Learn about timeouts",
        "level": 3
      },
      {
        "id": "api/commands/wrap#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/wrap#history",
        "text": "History",
        "level": 2
      },
      {
        "id": "api/commands/wrap#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "wrap"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Yield the object passed into `.wrap()`. If the object is a promise, yield its\nresolved value."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Syntax"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.wrap(subject)\ncy.wrap(subject, options)"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Usage"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Correct Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.wrap({ name: 'Jane Lane' })"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Arguments"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "subject (Object)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "An object to be yielded."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "options (Object)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Pass in an options object to change the default behavior of `cy.wrap()`."
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Option"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Default"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Description"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`log`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`true`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Displays the command in the "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/open-mode.md#Command-Log",
                    "children": [
                      {
                        "type": "text",
                        "value": "Command log"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`timeout`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/configuration.md#Timeouts",
                    "children": [
                      {
                        "type": "text",
                        "value": "`defaultCommandTimeout`"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Time to wait for `cy.wrap()` to resolve before "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "#Timeouts",
                    "children": [
                      {
                        "type": "text",
                        "value": "timing out"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Yields "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management",
            "children": [
              {
                "type": "text",
                "value": "Learn about subject management"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.wrap()` yields the object it was called with."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Examples"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Objects"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Invoke the function on the subject in wrap and return the new value"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "const getName = () => {\n  return 'Jane Lane'\n}\n\ncy.wrap({ name: getName }).invoke('name').should('eq', 'Jane Lane') // true"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Elements"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Wrap elements to continue executing commands"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.get('form').within(($form) => {\n  // ... more commands\n\n  cy.wrap($form).should('have.class', 'form-container')\n})"
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Conditionally wrap elements"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.get('button').then(($button) => {\n  // $button is a wrapped jQuery element\n  if ($button.someMethod() === 'something') {\n    // wrap this element so we can\n    // use cypress commands on it\n    cy.wrap($button).click()\n  } else {\n    // do something else\n  }\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Promises"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "You can wrap promises returned by the application code. Cypress commands will\nautomatically wait for the promise to resolve before continuing with the yielded\nvalue to the next command or assertion. See the\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/references/recipes.md#Logging-In",
            "children": [
              {
                "type": "text",
                "value": "Logging in using application code"
              }
            ]
          },
          {
            "type": "text",
            "value": " recipe for the\nfull example."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Simple example"
          }
        ]
      },
      {
        "type": "code",
        "lang": "js",
        "meta": null,
        "value": "const myPromise = new Promise((resolve, reject) => {\n  // we use setTimeout(...) to simulate async code.\n  setTimeout(() => {\n    resolve({\n      type: 'success',\n      message: 'It worked!',\n    })\n  }, 2500)\n})\n\nit('should wait for promises to resolve', () => {\n  cy.wrap(myPromise).its('message').should('eq', 'It worked!')\n})"
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Application example "
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "// import application code for logging in\nimport { userService } from '../../src/_services/user.service'\n\nit('can assert against resolved object using .should', () => {\n  cy.log('user service login')\n  cy.env(['username', 'password']).then(({ username, password }) => {\n    // wrap the promise returned by the application code\n    cy.wrap(userService.login(username, password))\n      // check the yielded object\n      .should('be.an', 'object')\n      .and('have.keys', ['firstName', 'lastName', 'username', 'id', 'token'])\n      .and('contain', {\n        username,\n        firstName: 'Test',\n        lastName: 'User',\n      })\n  })\n\n  // cy.visit command will wait for the promise returned from\n  // the \"userService.login\" to resolve. Then local storage item is set\n  // and the visit will immediately be authenticated and logged in\n  cy.visit('/')\n  // we should be logged in\n  cy.contains('Hi Test!').should('be.visible')\n})"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Note: `.wrap()` will not synchronize asynchronous function calls for you.\nFor example, given the following example:"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "You have two async functions `async function foo() {...}` and\n`async function bar() {...}`"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "You need to make sure `foo()` has resolved first before invoking `bar()`"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`bar()` is also dependent on some data that is created while after calling\nother Cypress commands."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "If you wrap the asynchronous\nfunctions in `cy.wrap()`, then `bar()` may be called prematurely before the\nrequired data is available:"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.wrap(foo())\n\ncy.get('some-button').click()\ncy.get('some-input').type(someValue)\ncy.get('some-submit-button').click()\n\n// this will execute `bar()` immediately without waiting\n// for other cy.get(...) functions to complete\ncy.wrap(bar()) // DON'T DO THIS"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "This behavior is due to the function invocation `foo()` and `bar()`, which call\nthe functions immediately to return a Promise."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "If you want `bar()` to execute after\n`foo()` and the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/get.md",
            "children": [
              {
                "type": "text",
                "value": "cy.get()"
              }
            ]
          },
          {
            "type": "text",
            "value": " commands, one solution is to chain\noff the final command using "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/then.md",
            "children": [
              {
                "type": "text",
                "value": ".then()"
              }
            ]
          },
          {
            "type": "text",
            "value": ":"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.wrap(foo())\n\ncy.get('some-button').click()\ncy.get('some-input').type(someValue)\ncy.get('some-submit-button')\n  .click()\n  .then(() => {\n    // this will execute `bar()` after the\n    // other cy.get(...) functions complete\n    cy.wrap(bar())\n  })"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Rules"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Requirements "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands",
            "children": [
              {
                "type": "text",
                "value": "Learn about chaining commands"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.wrap()` requires being chained off of `cy`."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Assertions "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions",
            "children": [
              {
                "type": "text",
                "value": "Learn about assertions"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.wrap()`, when its argument is a promise, will automatically wait until the\npromise resolves. If the promise is rejected, `cy.wrap()` will fail the test."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.wrap()` will automatically "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/retry-ability.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "retry"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": "\nuntil all chained assertions have passed."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Timeouts "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts",
            "children": [
              {
                "type": "text",
                "value": "Learn about timeouts"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.wrap()` can time out waiting for assertions you've added to pass."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Command Log"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Make assertions about object"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.wrap({ amount: 10 }).should('have.property', 'amount').and('eq', 10)"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The commands above will display in the Command Log as:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "When clicking on the `wrap` command within the command log, the console outputs\nthe following:"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "History"
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Version"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Changes"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/changelog.md#3-2-0",
                    "children": [
                      {
                        "type": "text",
                        "value": "3.2.0"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Retry `cy.wrap()` if `undefined` when followed by "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/should.md",
                    "children": [
                      {
                        "type": "text",
                        "value": ".should()"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/changelog.md#0-4-5",
                    "children": [
                      {
                        "type": "text",
                        "value": "0.4.5"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.wrap()` command added"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "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/commands/invoke.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`.invoke()`"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/its.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`.its()`"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/should.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`.should()`"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/spread.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`.spread()`"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/then.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`.then()`"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/recipes.md#Logging-In",
                    "children": [
                      {
                        "type": "text",
                        "value": "Logging In: Using application code"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": " recipe"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/recipes.md#Unit-Testing",
                    "children": [
                      {
                        "type": "text",
                        "value": "Unit Testing: Application Code"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": " recipe"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "token_estimate": 1027
}