Cypress中onCall()断言支持未文档化,请问确切支持哪些断言?
onCall() Method Great question! Let’s clear up the confusion between Sinon methods and Cypress-supported assertions, since that’s where the mix-up is happening.
First: onCall() isn’t an assertion—it’s a Sinon method
The onCall() you saw in the Cypress example is part of Sinon.js, not an assertion. Cypress actually bundles Sinon directly into its environment, so you get full access to all of Sinon’s stub, spy, and mock functionality. That includes methods like:
onCall(n): Defines behavior for the nth call to a stub/spyreturns(value): Sets a stub’s return valuethrows(error): Makes a stub throw an error when calledcallThrough(): Lets a stub call the original function
These are used to set up test doubles (stubs/spies) before you run assertions—they’re not part of the "assertions" list in the Cypress docs.
What Assertions Does Cypress Actually Support?
Cypress builds on top of Chai and adds several assertion libraries to cover different testing needs. Here’s the full breakdown:
1. Core Chai Assertions
These are the basic Chai assertions you’d use for general value checks:
expect(1 + 1).to.equal(2)expect('hello').to.include('ell')expect({ foo: 'bar' }).to.have.property('foo')
2. sinon-chai Assertions
This is the integration between Sinon and Chai that the Cypress docs mention. These assertions let you verify how your Sinon spies/stubs were called:
expect(mySpy).to.have.been.calledexpect(myStub).to.have.been.calledWith('user-id-123')expect(apiSpy).to.have.callCount(3)expect(formSubmitStub).to.have.been.calledOnceWithExactly({ name: 'Jane' })
3. Chai-jQuery Assertions
Built for testing DOM elements, these let you make assertions about elements’ state or content:
expect($('.submit-btn')).to.be.disabledexpect($('#modal')).to.be.visibleexpect($('.user-card')).to.contain('John Doe')expect($('input[name="email"]')).to.have.value('john@example.com')
4. Chai-as-Promised Assertions
For testing asynchronous Promise-based code:
expect(fetchUser()).to.eventually.have.property('id', 456)expect(rejectingPromise()).to.be.rejectedWith('Invalid credentials')
Quick Example to Tie It All Together
Here’s how you’d use onCall() (a Sinon method) alongside a sinon-chai assertion in Cypress:
// Set up a stub with onCall() to return different values on each call const getDataStub = cy.stub(window.api, 'getData') .onCall(0).returns({ data: 'first load' }) .onCall(1).returns({ data: 'second load' }) // Trigger the calls cy.get('#refresh-btn').click() cy.get('#refresh-btn').click() // Use a sinon-chai assertion to verify the stub was called twice expect(getDataStub).to.have.been.calledTwice
To sum up: Cypress supports all Sinon’s core functionality for creating stubs/spies, and its official assertion suite includes core Chai, sinon-chai, Chai-jQuery, and Chai-as-Promised. The onCall() method falls under Sinon’s setup tools, not the assertion category, which is why it’s not listed in the docs’ assertion section.
内容的提问来源于stack exchange,提问作者musicformellons

