Skip to content

sinon matchers not working as expected in withArgs #1328

@brucewpaul

Description

@brucewpaul

Current behavior:

// this returns true
const callback = sinon.stub(window, 'fetch');
callback.withArgs(sinon.match.any).returns(true);

console.log(window.fetch('/api'));

// this never matches and serverStub is never called
cy.stub(win, 'fetch').withArgs(sinon.matches.any).callsFake(serverStub)

Desired behavior:

To be able to use withArgs the same way you would be able to as in sinon

How to reproduce:

see above

Test code:

import sinon from 'sinon';

const stub = {
  '/api/v2/stub': { data: 'test stub' },
};

function responseStub(result) {
  return {
    text() {
      return Promise.resolve(result);
    },
    ok: true,
  };
}

Cypress.Commands.add('visitStubbed', (location, options = {}) => {

  // this works as intended and console logs true
  let callback = sinon.stub(window, 'fetch');
  callback.withArgs(sinon.match.any).returns(true);

  console.log(window.fetch('/api')); // Returns true

  function serverStub(url, req) {
    // this never gets called
    const resultStub = stub[url];

    if (resultStub) {
      return Promise.resolve(responseStub(resultStub));
    }

    return fetch(url)
  }

  cy.visit(location, {
    onBeforeLoad: (win) => {
      cy.stub(win, 'fetch')
        .withArgs(sinon.match.any)
        .callsFake(serverStub);

      win.fetch.callThrough();
    },
  })
})

Additional Info (images, stack traces, etc)

  • Operating System: mac osx
  • Cypress Version: 1.4.2
  • Browser Version: 64

Metadata

Metadata

Labels

pkg/driverThis is due to an issue in the packages/driver directory

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions