0

I am struggling to stub methods coming from the simple-git library using Sinon.

Here the method I would like to test:

const git = require('simple-git');


module.exports.getGitPushCommandChildProcess = async (centralisedRepoFolderPath, branchName) => {
  logger.debug('#### executing getGitPushCommandChildProcess()');
  logger.debug(`  [arg1] - centralisedRepoFolderPath: ${centralisedRepoFolderPath}`);
  logger.debug(`  [arg2] - branchName: ${branchName}`);

  await git(centralisedRepoFolderPath)
    .push('origin', branchName);
};

This is called by another method that is invoked in this test:

   it('merge request creation should succeed when running locally', async () => {
        // this will not get set when run locally
        process.env.PRIMARY_MR_ORIGINATOR_USERNAME = '';
        mockfs({
          'exchange-config': mockfs.load('test/unit/input-files/auto-merge-request/exchange-config')
        });
            sandbox.stub(git(), 'push').callsFake(() => Promise.resolve('pushed was called'));
        const { mrOpened, executionErrors } = await autoMergeRequestModule.openNewMergeRequest(
          apiObject,
          mockConfig,
          false
        );

        assert.strictEqual(executionErrors.toString(), '');
        assert.strictEqual(mrOpened, true);
      });

The stub got completely ignored. What am I missing here?


UPDATE

I get that the problem here is that a new instance is created within the method, hence the stub is ignored. Hence I moved to proxyquire:

  const gitStub = sinon.stub().callsFake(() => Promise.resolve());
    proxyquire('../../../../src/publishing/auto-merge-request/lib/auto-merge-request-lib', {
      'simple-git': gitStub
    });

however, this does not work either. What am I missing?

user3727540
  • 922
  • 3
  • 11
  • 30

0 Answers0