sinonjs / sinon

Test spies, stubs and mocks for JavaScript.
https://sinonjs.org/
Other
9.63k stars 769 forks source link

withArgs is not working when using with proxyquire #2455

Closed shadowfish07 closed 2 years ago

shadowfish07 commented 2 years ago

my test code:

describe('test', () => {
  const s = sinon
    .stub()
    .withArgs('123')
    .callsFake(() => {
      console.log('in');
    });

  const testShell = proxyquire('../../../app/shell/test', {
    shelljs: {
      echo: s,
    },
  });

  testShell();
});

app/shell/test.js:

'use strict';
function shell() {
  const shell = require('shelljs');
  shell.config.verbose = true;
  shell.config.fatal = true;
  shell.echo('123');
  shell.echo('456');
}

module.exports = shell;

the output is:

in
in

it means withArgs is not working.However,if I do not use proxyquire:

const shelljs = require('shelljs');
describe('test', () => {
  sinon
    .stub(shelljs, 'echo')
    .withArgs('123')
    .callsFake(() => {
      console.log('in');
    });

  shell.echo('123');
  shell.echo('456');
});

the output is:

in

So withArgs is working...

fatso83 commented 2 years ago

Hi, thanks for the issue. This seems like more of an issue with how to integrate proxyquire and Sinon than an explicit bug in Sinon (at least I cannot see how this shows a bug in Sinon), so I am closing it, as it does not really belong on the bug tracker.


We are trying to keep the GitHub issues list tidy and focused on bugs and feature discussions. This ticket looks like a usage question; please post it to StackOverflow and tag it with sinon, so the bigger community can help answer your questions.

If you feel that your topic is an issue with Sinon, please open a new ticket and follow the guidelines for reporting an issue.