Using the NODE_ENV is nice simple solution but when you need more there is a great dependency injection module called rewire that adds a special setter and getter to modules so you can modify their behavior such as: inject mocks for other modules or globals like process, leak private variables
override variables within the module.<p>This is especially nice because you don't need to add any special if statements to the module code you just load your module under test using rewire instead of require.<p>// file: test-stats-rewire.js<p>var rewire = require('rewire');<p>var assert = require('assert');<p>var stats = rewire('./stats');<p>// leak private method sum for testing<p>var sum = stats.__get__('sum');<p>assert.equal(sum([1]), 1);<p>assert.equal(sum([1, 2, 3]), 6);