Dependency injection with 'require()'
Dependency injection with 'require()'
Unwire uses rewire to allow you to inject code.
However, it also manipulates
require.cache so that any files that depend on
it will be given the rewired version.
npm install --save-dev unwire
A module that uses a dependency you want to replace
var fs = require'fs';// normally this would fail// but in the test, we replace 'fs' with 'mock_fs'return fsreadFileSync'some_file_that_does_not_exist.txt';;
A module that uses a rewired module
// usually readFile is actually readFile// but in the test, it is the rewired version of readFilevar readFile = require'./readFile';return readFile;;
Testing source/main.js but using a mocked fs
var assert = require'assert';var unwire = require'unwire';var mockFs =return 'some content';;describe'unwire'it'should replace readFile in ./source/main'var main = require'./source/main';// by default, main() should throw an errorassertthrowsmain /no such file or directory/;// remove ./source/main from require.cacheunwireflush'./source/main';// unwire readfile// => details on how to use rewirevar readFile = unwire'./source/readFile';readFile__set__'fs' mockFs;// load main// note that it now uses the rewired version of readFilemain = require'./source/main';assertmain === 'some content';;;
Having to rewire all the files and replace the dependencies.
var assert = require'assert';var rewire = require'rewire';var mockFs =return 'some content';;// we have to rewire both readFile and main// even though we only want to change readFilevar readFile = rewire'./source/readFile';var main = rewire'./source/main';readFile__set__'fs' mockFs;main__set__'readFile' readFile;assertmain === 'some content';
Rewire a module and overwrite the original in
path(string) : the module to rewire
var unwire = require'unwire';var readFile = unwire'./source/readFile';readFile__set__'fs' mockFs;var main = require'./source/main';main;
require.cache with the original module.
- No parameters
var unwire = require'unwire';var original = require'./source/readFile';var modified = unwire'./source/readFile';require'./source/readFile' === modified; // truerequire'./source/readFile' === original; // falsereadFile__unwire__;require'./source/readFile' === modified; // falserequire'./source/readFile' === original; // true
Remove a module from
require.cache. If not path is specified, it flushes all
[path](string) : optional. The module to reset.
var unwire = require'unwire';// source/main is loaded into cache before readFile is unwiredrequire'./source/main';var readFile = unwire'./source/readFile';readFile__set__'fs' ;// source/main has already been cached, so it doesn't workvar main = require'./source/main';main; // throw error// source/main is removed from cacheunwireflush'./source/main';// source/main is reloaded and now uses the unwired version of readFilemain = require'./source/main';main; // success!
The MIT License (MIT)
Copyright (c) 2014 George Czabania
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Fix bug with unwire().__unwire__ not always reverting to the original version
- Move code into lib/
- Split code into two files. One with logic and the other with the public api.
- Update docs with better examples
- Add MIT license
- Remove self from require.cache so that module.parent is always up to date
module.parentto detect where unwire is being called from
- Add readme
- Add better tests
- Add unwire.flush();
- Initial commit