theKashey/rewiremock

Please add a test for a module that returns a Class that gets newed up by the SUT

blmille1 opened this issue · 1 comments

I've been wrestling with this example and have created an article outlining what I did to get it to work.
That article is available here: https://coding-art.blogspot.com/2021/03/how-to-test-nodejs-module-and-swap-out.html

I've seen many people ask for this example and I don't know of any that have received a satisfying answer.

I think that if you add this to your tests, we will be satisfied.

Thanks a lot!
(We use the pattern of a module returning a class that is later newed up quite a bit)

The request is not very clear. You are not doing anything except mocking the dependency of a SUT, and what it is - class, function, or a variable - does not matter at all.

Once upon a time I've written an article where the majority of the mocking cases, including classes, have been covered - https://dev.to/thekashey/please-stop-playing-with-proxyquire-11j4

You also have a little problem with your example (no .disable), as well as you can use more compact code.

- rewiremock('../../../real/code/my-real-class').with(FakeDep);
- rewiremock.enable();
- const sut = require('../../../real/code/my-sut');
you forgot to disable
+ const sut = rewiremock.proxy(() => require('../../../real/code/my-sut', {
+ '../../../real/code/my-real-class': FakeDep
+});