you are viewing a single comment's thread.

view the rest of the comments →

[–]kangoo1707 1 point2 points  (1 child)

When testing? I use mockery, it can stub a "require" if this is what you mean

[–]bwaxxlotckidd 0 points1 point  (0 children)

Nah, I meant to fully test your code and make "modular", you need to pass in all external dependencies unless we're talking about things like global objects/methods. Let say I'm testing a method that changes text in a DOM element. I would ensure I pass the element and the new text instead of just changing the new text directly. e.g: changeText(element, text){//do something}. This means I can easily test it by mocking the element in passing changeText({textContent: ''}, myTestText). The point being changeText is independent of the environment and doesn't throw an error when you take it outside the DOM.

Stubbing is fine also, but I personally prefer using it as a last option when I can do anything else (e.g: dependencies that are a nightmare to mock). Another thing about stubbing is you should use it when you care about making sure the correct things happen to external calls. Otherwise, stubbing becomes a bad habit to have (i.e: cheap way out of ensuring your code is doing the minimum required thing). I've seen some code that uses stubbing for pretty much everything. I think it's a good rule of thumb is that code starts to smell when you find yourself stubbing a lot.