I am struggling to write high quality tests around my node modules. The problem is the require module system. I want to be able to check that a certain required module has a method or its state has changed. There seem to be 2 relatively small libraries which can be used here: node-gently and mockery. However, due to their low ‘profile’ it makes me think that either people don’t test this, or there is another way of doing this that I am not aware of.
What is the best way to mock out and test a module that has been required?
———– UPDATE —————
node-sandbox works on the same principals as stated below but is wrapped up in a nice module. I am finding it very nice to work with.
————— detailed awnser —————
After much trial I have found the best way to test node modules in isolation while mocking things out is to use the method by Vojta Jina to run each module inside of a vm with a new context as explained here.
with this testing vm module:
it is possible to test each module with its own context and easily stub out all external dependencys.
I highly recommend this way for writing effective tests in isolation. Only acceptance tests should hit the entire stack. Unit and integration tests should test isolated parts of the system.