theKashey / rewiremock

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

Using mock trackers #150

Closed darcyrush closed 5 months ago

darcyrush commented 5 months ago

I have figured out how to mock the dependencies in my SUTs using this library, but I want to to actually use some sort of mock tracker to test calls etc. I cannot figure out how to include them from this library?

import rewiremock from "rewiremock";

const mock = rewiremock.mock() // This isn't a thing
const sut = rewiremock.proxy<any>(
  './path/to/sut.ts',
  {
    './dependency': { mock }
  }
)

sut.func()

assert(mock.called.once, true)
theKashey commented 5 months ago

rewiremock does not defines own helpers for this and proposing you to use something different, like sinon. You may tell rewritemock which solution to use via rewiremock.stubFactory, but that's all.

import sinon from 'sinon';
import rewiremock from 'rewiremock';

const mock = sinon.spy();
const sut = rewiremock.proxy<any>(
  './path/to/sut.ts',
  {
    './dependency': { mock }
  }
);

sut.func();

sinon.assert.calledOnce(mock);
darcyrush commented 5 months ago

Thank you for the quick response and clarification, noted.