Proxyquire, rewire, SandboxedModule and Sinon: pros and cons

When taunting Node dependencies, I met in the following libraries:

All of them seem to do the same thing more or less: let you make fun of calls to require() (with the exception of Sinon, which scoffs at anything). It seems that all of them require a rather complicated setup, noting the exact syntax of the string passed to require is small when refactoring.

What are the pros and cons of each library? When will I choose one by one? What are some sample uses for each library? What other products in this space are better?

+50
mocking sinon proxyquire
Jun 12 '14 at 17:02
source share
1 answer

This sounds like cheating, but since no one answers this question, it says:

  • Proxyquire makes a commitment and allows you to introduce fakes anywhere in the dependency chain. This requires that you do not get the upper hand and do not use methods that you do not define, because you need to take responsibility, they will return to the original. This can be disabled using noCallThru . So it still loads the original, just replaces things with what you define. Unlike Rewire and SandboxedModule, you cannot define global variables for your overloads.

  • Revix takes over the requirement and introduces the __get__ and __set__ into each module. If you know the name of a private variable, you can replace it. Think about dependency injection.

  • SandboxedModule is almost identical to Proxyquire, except that it runs the entire process in the new V8 vm. (There is a cost of execution for each test for this approach.) It also has an unpleasant bug in version 1.0, which causes it to fail when something that you have not replaced with links does not support its own module, which it does not support. See https://github.com/robrich/sandboxed-module-graceful-fs .

  • Sinon does not take it upon himself like the other 3. Rather, it is a more traditional mockery. Replace the specified methods with a fake or create a layout that tracks when it was called.

+98
Jun 15
source share



All Articles