There seems to be a node package for nearly every conceivable need, and so I had assumed that mocking–creating modules that exposed the same API as another module, but behaved in a predictable and controllable way so you can test another component–was going to be as simple as npm install . But the packages I found seemed overly complicated, poorly documented, and didn’t really work very well. So I tried the simplest solution I could think of:
1) In your test config.js, create an entry for mock modules, then create an entry for every module you need to mock, like:
2) In your other environments, make similar entries, pointing to either the mock module or the real module as needed.
3) For each module that requires one of these mockable modules, just use a variable name, like:
var config require(‘config’);
var smtpMail require(config.mockModules.smtpMail);
This is working well for me, and I haven’t hit any downside. Obviously, config needs to be defined before it’s used. Everything is managed through the config, and that’s the only place you need to make changes between different environments.