mfncooper / mockery

Simplifying the use of mocks with Node.js
Other
1.1k stars 60 forks source link

How to just intercept a few require calls. #56

Closed wuyiqun0718 closed 7 years ago

wuyiqun0718 commented 7 years ago

Hi all, I'm very new to unit testing, and I'm trying to get familiar with these mock libraries.

I tried to not use the enable function when setup and only do registerMock for some packages, but it's not working for me. I'm guessing the enable function is a must, so is there anyway to just register mock for some module instead of intercepting all of them? I also tried rewire, but apparently it can't intercept any require calls.

davglass commented 7 years ago

I don't quite follow.. When you call mockery.enable() it will then mock only the modules that are set with mockery.registerMock. It doesn't mock everything, only what is sent to that method.

wuyiqun0718 commented 7 years ago

OK, I got too many require calls in my file. I tried different ways to mock them and it kept giving me different errors. So I thought it's because I didn't mock some packages. And since I got the confirmation from you, I just tried to modify my mock module again and it worked! Apparently I don't need to pass parameters to my mock functions when do registerMock.

Please close this, thank you for the answer!

davglass commented 7 years ago

Glad you got it working..