regexcypresssinonassertionsinon-chai

Cypress: assert argument of stubbed function with Regex


I have a stubbed method that is having the following structure printed in the Cypress console:

myMethod('start', Object{5})

I know that the object has a key, segmentB -> when console logging it in the stub, I see it but I do not want to start making assertions in the stub

I want to assert, that the value of segmentB starts with 'MPI_'

I though of combining "should be called with match" and Cypress.sinon assertions like the following, but it is not working.

 cy
        .get('@myMethod')
        .should('be.calledWithMatch', 'start', {
          segmentB: Cypress.sinon.match(/^MPI_/)
      })

.should('beCalledWithMatch', 'start') or asserting key/value pairs of the objects without variable parts works, but I'd appreciate any help for asserting with a regex.


Solution

  • It works for me, here is a simple reproduction test that passes.

    it('uses calledWithMatch assertion', () => {
      
      const wrapper = {
        myMethod: function (param1, param2) {
          console.log('Called with ', param1, param2)
        }
      }
    
      cy.spy(wrapper, 'myMethod').as('myMethod')
      wrapper.myMethod('start', {segmentB: 'MPI_abc'})
    
      cy.get('@myMethod')
        .should('be.calledWithMatch', 'start', {
          segmentB: Cypress.sinon.match(/^MPI_/)       // ✅ passes
      })
    })