{
  "doc": {
    "id": "api/commands/spy",
    "title": "spy | Cypress Documentation",
    "description": "Wrap a method in a spy in order to record calls to and arguments of the function in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/spy.md",
    "version": "e6988a974973e9090ce70406c38cb2b9e0eac9fa",
    "updated_at": "2026-05-15T15:50:22.536Z",
    "headings": [
      {
        "id": "api/commands/spy#spy",
        "text": "spy",
        "level": 1
      },
      {
        "id": "api/commands/spy#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/spy#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/spy#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/spy#yields-learn-about-subject-management",
        "text": "Yields Learn about subject management",
        "level": 3
      },
      {
        "id": "api/commands/spy#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/spy#method",
        "text": "Method",
        "level": 3
      },
      {
        "id": "api/commands/spy#wrap-a-method-with-a-spy",
        "text": "Wrap a method with a spy",
        "level": 4
      },
      {
        "id": "api/commands/spy#disable-logging-to-command-log",
        "text": "Disable logging to Command Log",
        "level": 4
      },
      {
        "id": "api/commands/spy#more-cy-spy-examples",
        "text": "More cy.spy() examples",
        "level": 4
      },
      {
        "id": "api/commands/spy#aliases",
        "text": "Aliases",
        "level": 3
      },
      {
        "id": "api/commands/spy#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/spy#restores",
        "text": "Restores",
        "level": 3
      },
      {
        "id": "api/commands/spy#automatic-reset-restore-between-tests",
        "text": "Automatic reset/restore between tests",
        "level": 4
      },
      {
        "id": "api/commands/spy#differences",
        "text": "Differences",
        "level": 3
      },
      {
        "id": "api/commands/spy#difference-between-cy-spy-and-cy-stub",
        "text": "Difference between cy.spy() and cy.stub()",
        "level": 4
      },
      {
        "id": "api/commands/spy#assertions",
        "text": "Assertions",
        "level": 3
      },
      {
        "id": "api/commands/spy#assertion-support",
        "text": "Assertion Support",
        "level": 4
      },
      {
        "id": "api/commands/spy#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/spy#history",
        "text": "History",
        "level": 2
      },
      {
        "id": "api/commands/spy#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/commands/spy#syntax",
      "doc_id": "api/commands/spy",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n```\ncy.spy(object, method)\n```\n\n### Usage\n\n**Correct Usage**\n\n```\ncy.spy(user, 'addFriend')cy.spy(user, 'addFriend').as('addFriend')\n```\n\n### Arguments\n\n**object _(Object)_**\n\nThe `object` that has the `method` to be wrapped.\n\n**method _(String)_**\n\nThe name of the `method` on the `object` to be wrapped.\n\n### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)\n\n*   `cy.spy()` is _synchronous_ and returns a value (the spy) instead of a Promise-like chain-able object. It can be aliased.\n*   `cy.spy()` returns a [Sinon.js spy](https://sinonjs.org/releases/v8/spies/). All methods found on Sinon.JS spies are supported.\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/commands/spy.json",
      "token_estimate": 108
    },
    {
      "id": "api/commands/spy#yields-learn-about-subject-management",
      "doc_id": "api/commands/spy",
      "heading": "Yields Learn about subject management",
      "heading_level": 3,
      "content_markdown": "### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)\n\n*   `cy.spy()` is _synchronous_ and returns a value (the spy) instead of a Promise-like chain-able object. It can be aliased.\n*   `cy.spy()` returns a [Sinon.js spy](https://sinonjs.org/releases/v8/spies/). All methods found on Sinon.JS spies are supported.\n",
      "section": "api",
      "anchors": [
        "yields-learn-about-subject-management"
      ],
      "path": "/llm/json/chunked/api/commands/spy.json",
      "token_estimate": 53
    },
    {
      "id": "api/commands/spy#examples",
      "doc_id": "api/commands/spy",
      "heading": "Examples",
      "heading_level": 2,
      "content_markdown": "## Examples\n\n### Method\n\n#### Wrap a method with a spy\n\n```\n// assume App.start calls util.addListenerscy.spy(util, 'addListeners')App.start()expect(util.addListeners).to.be.called\n```\n\n#### Disable logging to Command Log\n\nYou can chain a `.log(bool)` method to disable `cy.spy()` calls from being shown in the Command Log. This may be useful when your stubs are called an excessive number of times.\n\n```\nconst obj = {  foo() {},}const spy = cy.spy(obj, 'foo').log(false)\n```\n\n#### More `cy.spy()` examples\n\n[Check out our example recipe testing spying, stubbing and time](/llm/markdown/app/references/recipes.md#Stubbing-and-spying)\n\n### Aliases\n\nYou can alias spies, similar to how [`.as()`](/llm/markdown/api/commands/as.md) works. This can make your spies easier to identify in error messages and Cypress's command log, and allows you to assert against them later using `cy.get()`.\n\n```\nconst obj = {  foo() {},}const spy = cy.spy(obj, 'foo').as('anyArgs')const withFoo = spy.withArgs('foo').as('withFoo')obj.foo()expect(spy).to.be.calledcy.get('@withFoo').should('be.called') // purposefully failing assertion\n```\n\nYou will see the following in the command log:\n",
      "section": "api",
      "anchors": [
        "examples"
      ],
      "path": "/llm/json/chunked/api/commands/spy.json",
      "token_estimate": 195
    },
    {
      "id": "api/commands/spy#method",
      "doc_id": "api/commands/spy",
      "heading": "Method",
      "heading_level": 3,
      "content_markdown": "### Method\n\n#### Wrap a method with a spy\n\n```\n// assume App.start calls util.addListenerscy.spy(util, 'addListeners')App.start()expect(util.addListeners).to.be.called\n```\n\n#### Disable logging to Command Log\n\nYou can chain a `.log(bool)` method to disable `cy.spy()` calls from being shown in the Command Log. This may be useful when your stubs are called an excessive number of times.\n\n```\nconst obj = {  foo() {},}const spy = cy.spy(obj, 'foo').log(false)\n```\n\n#### More `cy.spy()` examples\n\n[Check out our example recipe testing spying, stubbing and time](/llm/markdown/app/references/recipes.md#Stubbing-and-spying)\n",
      "section": "api",
      "anchors": [
        "method"
      ],
      "path": "/llm/json/chunked/api/commands/spy.json",
      "token_estimate": 107
    },
    {
      "id": "api/commands/spy#disable-logging-to-command-log",
      "doc_id": "api/commands/spy",
      "heading": "Disable logging to Command Log",
      "heading_level": 4,
      "content_markdown": "#### Disable logging to Command Log\n\nYou can chain a `.log(bool)` method to disable `cy.spy()` calls from being shown in the Command Log. This may be useful when your stubs are called an excessive number of times.\n\n```\nconst obj = {  foo() {},}const spy = cy.spy(obj, 'foo').log(false)\n```\n",
      "section": "api",
      "anchors": [
        "disable-logging-to-command-log"
      ],
      "path": "/llm/json/chunked/api/commands/spy.json",
      "token_estimate": 65
    },
    {
      "id": "api/commands/spy#aliases",
      "doc_id": "api/commands/spy",
      "heading": "Aliases",
      "heading_level": 3,
      "content_markdown": "### Aliases\n\nYou can alias spies, similar to how [`.as()`](/llm/markdown/api/commands/as.md) works. This can make your spies easier to identify in error messages and Cypress's command log, and allows you to assert against them later using `cy.get()`.\n\n```\nconst obj = {  foo() {},}const spy = cy.spy(obj, 'foo').as('anyArgs')const withFoo = spy.withArgs('foo').as('withFoo')obj.foo()expect(spy).to.be.calledcy.get('@withFoo').should('be.called') // purposefully failing assertion\n```\n\nYou will see the following in the command log:\n",
      "section": "api",
      "anchors": [
        "aliases"
      ],
      "path": "/llm/json/chunked/api/commands/spy.json",
      "token_estimate": 85
    },
    {
      "id": "api/commands/spy#notes",
      "doc_id": "api/commands/spy",
      "heading": "Notes",
      "heading_level": 2,
      "content_markdown": "## Notes\n\n### Restores\n\n#### Automatic reset/restore between tests\n\n`cy.spy()` creates spies in a [sandbox](https://sinonjs.org/releases/v8/sandbox/), so all spies created are automatically reset/restored between tests without you having to explicitly reset/restore them.\n\n### Differences\n\n#### Difference between cy.spy() and cy.stub()\n\nThe main difference between `cy.spy()` and [`cy.stub()`](/llm/markdown/api/commands/stub.md) is that `cy.spy()` does not replace the method, it only wraps it. So, while invocations are recorded, the original method is still called. This can be very useful when testing methods on native browser objects. You can verify a method is being called by your test and still have the original method action invoked.\n\n### Assertions\n\n#### Assertion Support\n\nCypress has also built-in [Sinon-Chai](/llm/markdown/app/references/bundled-libraries.md#Sinon-Chai) support, so any [assertions supported by `Sinon-Chai`](/llm/markdown/app/references/assertions.md#Sinon-Chai) can be used without any configuration.\n",
      "section": "api",
      "anchors": [
        "notes"
      ],
      "path": "/llm/json/chunked/api/commands/spy.json",
      "token_estimate": 164
    },
    {
      "id": "api/commands/spy#differences",
      "doc_id": "api/commands/spy",
      "heading": "Differences",
      "heading_level": 3,
      "content_markdown": "### Differences\n\n#### Difference between cy.spy() and cy.stub()\n\nThe main difference between `cy.spy()` and [`cy.stub()`](/llm/markdown/api/commands/stub.md) is that `cy.spy()` does not replace the method, it only wraps it. So, while invocations are recorded, the original method is still called. This can be very useful when testing methods on native browser objects. You can verify a method is being called by your test and still have the original method action invoked.\n",
      "section": "api",
      "anchors": [
        "differences"
      ],
      "path": "/llm/json/chunked/api/commands/spy.json",
      "token_estimate": 92
    },
    {
      "id": "api/commands/spy#difference-between-cy-spy-and-cy-stub",
      "doc_id": "api/commands/spy",
      "heading": "Difference between cy.spy() and cy.stub()",
      "heading_level": 4,
      "content_markdown": "#### Difference between cy.spy() and cy.stub()\n\nThe main difference between `cy.spy()` and [`cy.stub()`](/llm/markdown/api/commands/stub.md) is that `cy.spy()` does not replace the method, it only wraps it. So, while invocations are recorded, the original method is still called. This can be very useful when testing methods on native browser objects. You can verify a method is being called by your test and still have the original method action invoked.\n",
      "section": "api",
      "anchors": [
        "difference-between-cy-spy-and-cy-stub"
      ],
      "path": "/llm/json/chunked/api/commands/spy.json",
      "token_estimate": 89
    },
    {
      "id": "api/commands/spy#command-log",
      "doc_id": "api/commands/spy",
      "heading": "Command Log",
      "heading_level": 2,
      "content_markdown": "## Command Log\n\n**_Create a spy, alias it, and call it_**\n\n```\nconst obj = {  foo() {},}const spy = cy.spy(obj, 'foo').as('foo')obj.foo('foo', 'bar')expect(spy).to.be.called\n```\n\nThe command above will display in the Command Log as:\n\nWhen clicking on the `spy-1` event within the command log, the console outputs the following:\n",
      "section": "api",
      "anchors": [
        "command-log"
      ],
      "path": "/llm/json/chunked/api/commands/spy.json",
      "token_estimate": 65
    }
  ]
}