theKashey / rewiremock

The right way to mock dependencies in Node.js or webpack environment.
MIT License
491 stars 31 forks source link

Invoking 'proxy' to generate test object but stub not invoked #140

Open zlaroche opened 2 years ago

zlaroche commented 2 years ago

There must be something simple I'm doing wrong, but I can't figure it out.

A.js:

const { func } = require('./B')

const execFunc = () => {
  func();
}

module.exports = { execFunc }

B.js:

const func = () => {
  console.log("This is not the stubbed function");
}

module.exports = { func };

test.js:

const rewiremock = require('rewiremock/node');
rewiremock.overrideEntryPoint(module);

describe('rewiremock demonstration', () => {
  test('stub function with rewiremock', () => {

    const mockFunc = () => {
      console.log('This is the stubbed function')
    };

    const subject = rewiremock.proxy(
      () => require('./A'),
      () => {
        rewiremock(() => require('./B'))
          .with({ func: mockFunc })
      });
    subject.execFunc();
  })
})

output:

 This is not the stubbed function
theKashey commented 2 years ago

So what was the problem?

zlaroche commented 2 years ago

I'm sorry, I should not have closed the issue. In my case I was able to get the mocking I needed working with sinon, but I still did not solve it for the code above.

theKashey commented 1 year ago

The code above works well. I've added test for "issue 140", but it's totally green