{
  "doc": {
    "id": "api/commands/stub",
    "title": "stub | Cypress Documentation",
    "description": "Replace a function, record its usage and control its behavior in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/stub.md",
    "version": "a8fd16711bdda4c7b5645b9717e588ae99ec2470",
    "updated_at": "2026-05-18T17:21:32.047Z",
    "headings": [
      {
        "id": "api/commands/stub#stub",
        "text": "stub",
        "level": 1
      },
      {
        "id": "api/commands/stub#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/stub#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/stub#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/stub#yields-learn-about-subject-management",
        "text": "Yields Learn about subject management",
        "level": 3
      },
      {
        "id": "api/commands/stub#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/stub#method",
        "text": "Method",
        "level": 3
      },
      {
        "id": "api/commands/stub#create-a-stub-and-manually-replace-a-function",
        "text": "Create a stub and manually replace a function",
        "level": 4
      },
      {
        "id": "api/commands/stub#replace-a-method-with-a-stub",
        "text": "Replace a method with a stub",
        "level": 4
      },
      {
        "id": "api/commands/stub#replace-a-method-with-a-function",
        "text": "Replace a method with a function",
        "level": 4
      },
      {
        "id": "api/commands/stub#specify-the-return-value-of-a-stubbed-method",
        "text": "Specify the return value of a stubbed method",
        "level": 4
      },
      {
        "id": "api/commands/stub#replace-built-in-window-methods-like-prompt",
        "text": "Replace built-in window methods like prompt",
        "level": 4
      },
      {
        "id": "api/commands/stub#disable-logging-to-command-log",
        "text": "Disable logging to Command Log",
        "level": 4
      },
      {
        "id": "api/commands/stub#more-cy-stub-examples",
        "text": "More cy.stub() examples",
        "level": 4
      },
      {
        "id": "api/commands/stub#aliases",
        "text": "Aliases",
        "level": 3
      },
      {
        "id": "api/commands/stub#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/stub#restores",
        "text": "Restores",
        "level": 3
      },
      {
        "id": "api/commands/stub#automatic-reset-restore-between-tests",
        "text": "Automatic reset/restore between tests",
        "level": 4
      },
      {
        "id": "api/commands/stub#differences",
        "text": "Differences",
        "level": 3
      },
      {
        "id": "api/commands/stub#difference-between-cy-spy-and-cy-stub",
        "text": "Difference between cy.spy() and cy.stub()",
        "level": 4
      },
      {
        "id": "api/commands/stub#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/stub#history",
        "text": "History",
        "level": 2
      },
      {
        "id": "api/commands/stub#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/commands/stub#syntax",
      "doc_id": "api/commands/stub",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n```\ncy.stub()cy.stub(object, method)\n```\n\n### Usage\n\n**Correct Usage**\n\n```\ncy.stub(user, 'addFriend')cy.stub(user, 'addFriend').as('addFriend')\n```\n\n### Arguments\n\n**object _(Object)_**\n\nThe `object` that has the `method` to be replaced.\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.stub()` is _synchronous_ and returns a value (the stub) instead of a Promise-like chain-able object. It can be aliased.\n*   `cy.stub()` returns a [Sinon.js stub](http://sinonjs.org). All methods found on [Sinon.js](http://sinonjs.org) spies and stubs are supported.\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/commands/stub.json",
      "token_estimate": 111
    },
    {
      "id": "api/commands/stub#yields-learn-about-subject-management",
      "doc_id": "api/commands/stub",
      "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.stub()` is _synchronous_ and returns a value (the stub) instead of a Promise-like chain-able object. It can be aliased.\n*   `cy.stub()` returns a [Sinon.js stub](http://sinonjs.org). All methods found on [Sinon.js](http://sinonjs.org) spies and stubs are supported.\n",
      "section": "api",
      "anchors": [
        "yields-learn-about-subject-management"
      ],
      "path": "/llm/json/chunked/api/commands/stub.json",
      "token_estimate": 56
    },
    {
      "id": "api/commands/stub#examples",
      "doc_id": "api/commands/stub",
      "heading": "Examples",
      "heading_level": 2,
      "content_markdown": "## Examples\n\n### Method\n\n#### Create a stub and manually replace a function\n\n```\n// assume App.start calls util.addListenersutil.addListeners = cy.stub()App.start()expect(util.addListeners).to.be.called\n```\n\n#### Replace a method with a stub\n\n```\n// assume App.start calls util.addListenerscy.stub(util, 'addListeners')App.start()expect(util.addListeners).to.be.called\n```\n\n#### Replace a method with a function\n\n```\n// assume App.start calls util.addListenerslet listenersAdded = falsecy.stub(util, 'addListeners').callsFake(() => {  listenersAdded = true})App.start()expect(listenersAdded).to.be.true\n```\n\n#### Specify the return value of a stubbed method\n\n```\n// assume App.start calls util.addListeners, which returns a function// that removes the listenersconst removeStub = cy.stub()cy.stub(util, 'addListeners').returns(removeStub)App.start()App.stop()expect(removeStub).to.be.called\n```\n\n#### Replace built-in window methods like prompt\n\nIn end-to-end tests, replacing built-in `window` methods needs to happen _after_ the page is visited, but _before_ the application under test is loaded. You can do this by stubbing functions inside the [`cy.visit()`](/llm/markdown/api/commands/visit.md) command `onBeforeLoad` function.\n\nIn Component tests, because the page isn't being reloaded, all you need to do is stub functions before mounting your component.\n\n*   End-to-End Test\n*   Component Test\n\n```\ncy.visit('http://localhost:3000', {  onBeforeLoad(win) {    // Stub your functions here    cy.stub(win, 'prompt').returns('my custom message')  },})App.start()cy.window().its('prompt').should('be.called')cy.get('.name').should('have.value', 'my custom message')\n```\n\n```\n// Stub your functions herecy.mount()cy.stub(window, 'prompt').returns('my custom message')// After that, mount your componentcy.mount(<MyComponent />)cy.window().its('prompt').should('be.called')cy.get('.name').should('have.value', 'my custom message')\n```\n\n#### Disable logging to Command Log\n\nYou can chain a `.log(bool)` method to disable `cy.stub()` 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 stub = cy.stub(obj, 'foo').log(false)\n```\n\n#### More `cy.stub()` 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 stubs, similar to how [`.as()`](/llm/markdown/api/commands/as.md) works. This can make your stubs 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 stub = cy.stub(obj, 'foo').as('anyArgs')const withFoo = stub.withArgs('foo').as('withFoo')obj.foo()expect(stub).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/stub.json",
      "token_estimate": 431
    },
    {
      "id": "api/commands/stub#method",
      "doc_id": "api/commands/stub",
      "heading": "Method",
      "heading_level": 3,
      "content_markdown": "### Method\n\n#### Create a stub and manually replace a function\n\n```\n// assume App.start calls util.addListenersutil.addListeners = cy.stub()App.start()expect(util.addListeners).to.be.called\n```\n\n#### Replace a method with a stub\n\n```\n// assume App.start calls util.addListenerscy.stub(util, 'addListeners')App.start()expect(util.addListeners).to.be.called\n```\n\n#### Replace a method with a function\n\n```\n// assume App.start calls util.addListenerslet listenersAdded = falsecy.stub(util, 'addListeners').callsFake(() => {  listenersAdded = true})App.start()expect(listenersAdded).to.be.true\n```\n\n#### Specify the return value of a stubbed method\n\n```\n// assume App.start calls util.addListeners, which returns a function// that removes the listenersconst removeStub = cy.stub()cy.stub(util, 'addListeners').returns(removeStub)App.start()App.stop()expect(removeStub).to.be.called\n```\n\n#### Replace built-in window methods like prompt\n\nIn end-to-end tests, replacing built-in `window` methods needs to happen _after_ the page is visited, but _before_ the application under test is loaded. You can do this by stubbing functions inside the [`cy.visit()`](/llm/markdown/api/commands/visit.md) command `onBeforeLoad` function.\n\nIn Component tests, because the page isn't being reloaded, all you need to do is stub functions before mounting your component.\n\n*   End-to-End Test\n*   Component Test\n\n```\ncy.visit('http://localhost:3000', {  onBeforeLoad(win) {    // Stub your functions here    cy.stub(win, 'prompt').returns('my custom message')  },})App.start()cy.window().its('prompt').should('be.called')cy.get('.name').should('have.value', 'my custom message')\n```\n\n```\n// Stub your functions herecy.mount()cy.stub(window, 'prompt').returns('my custom message')// After that, mount your componentcy.mount(<MyComponent />)cy.window().its('prompt').should('be.called')cy.get('.name').should('have.value', 'my custom message')\n```\n\n#### Disable logging to Command Log\n\nYou can chain a `.log(bool)` method to disable `cy.stub()` 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 stub = cy.stub(obj, 'foo').log(false)\n```\n\n#### More `cy.stub()` 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/stub.json",
      "token_estimate": 343
    },
    {
      "id": "api/commands/stub#replace-built-in-window-methods-like-prompt",
      "doc_id": "api/commands/stub",
      "heading": "Replace built-in window methods like prompt",
      "heading_level": 4,
      "content_markdown": "#### Replace built-in window methods like prompt\n\nIn end-to-end tests, replacing built-in `window` methods needs to happen _after_ the page is visited, but _before_ the application under test is loaded. You can do this by stubbing functions inside the [`cy.visit()`](/llm/markdown/api/commands/visit.md) command `onBeforeLoad` function.\n\nIn Component tests, because the page isn't being reloaded, all you need to do is stub functions before mounting your component.\n\n*   End-to-End Test\n*   Component Test\n\n```\ncy.visit('http://localhost:3000', {  onBeforeLoad(win) {    // Stub your functions here    cy.stub(win, 'prompt').returns('my custom message')  },})App.start()cy.window().its('prompt').should('be.called')cy.get('.name').should('have.value', 'my custom message')\n```\n\n```\n// Stub your functions herecy.mount()cy.stub(window, 'prompt').returns('my custom message')// After that, mount your componentcy.mount(<MyComponent />)cy.window().its('prompt').should('be.called')cy.get('.name').should('have.value', 'my custom message')\n```\n",
      "section": "api",
      "anchors": [
        "replace-built-in-window-methods-like-prompt"
      ],
      "path": "/llm/json/chunked/api/commands/stub.json",
      "token_estimate": 144
    },
    {
      "id": "api/commands/stub#disable-logging-to-command-log",
      "doc_id": "api/commands/stub",
      "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.stub()` 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 stub = cy.stub(obj, 'foo').log(false)\n```\n",
      "section": "api",
      "anchors": [
        "disable-logging-to-command-log"
      ],
      "path": "/llm/json/chunked/api/commands/stub.json",
      "token_estimate": 65
    },
    {
      "id": "api/commands/stub#aliases",
      "doc_id": "api/commands/stub",
      "heading": "Aliases",
      "heading_level": 3,
      "content_markdown": "### Aliases\n\nYou can alias stubs, similar to how [`.as()`](/llm/markdown/api/commands/as.md) works. This can make your stubs 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 stub = cy.stub(obj, 'foo').as('anyArgs')const withFoo = stub.withArgs('foo').as('withFoo')obj.foo()expect(stub).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/stub.json",
      "token_estimate": 85
    },
    {
      "id": "api/commands/stub#notes",
      "doc_id": "api/commands/stub",
      "heading": "Notes",
      "heading_level": 2,
      "content_markdown": "## Notes\n\n### Restores\n\n#### Automatic reset/restore between tests\n\n`cy.stub()` creates stubs in a [sandbox](http://sinonjs.org/releases/v8/sandbox/), so all stubs 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",
      "section": "api",
      "anchors": [
        "notes"
      ],
      "path": "/llm/json/chunked/api/commands/stub.json",
      "token_estimate": 133
    },
    {
      "id": "api/commands/stub#differences",
      "doc_id": "api/commands/stub",
      "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/stub.json",
      "token_estimate": 92
    },
    {
      "id": "api/commands/stub#difference-between-cy-spy-and-cy-stub",
      "doc_id": "api/commands/stub",
      "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/stub.json",
      "token_estimate": 89
    },
    {
      "id": "api/commands/stub#command-log",
      "doc_id": "api/commands/stub",
      "heading": "Command Log",
      "heading_level": 2,
      "content_markdown": "## Command Log\n\n**_Create a stub, alias it, and call it_**\n\n```\nconst obj = {  foo() {},}const stub = cy.stub(obj, 'foo').as('foo')obj.foo('foo', 'bar')expect(stub).to.be.called\n```\n\nThe command above will display in the Command Log as:\n\nWhen clicking on the `(stub-1)` event within the command log, the console outputs the following:\n",
      "section": "api",
      "anchors": [
        "command-log"
      ],
      "path": "/llm/json/chunked/api/commands/stub.json",
      "token_estimate": 65
    }
  ]
}