Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Easy monkey-patching for CodeRoad

License

NotificationsYou must be signed in to change notification settings

ShMcK/rewire-coderoad

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dependency injection for node.js applications.

rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may

  • introduce mocks for other modules
  • leak private variables
  • override variables within the module.

rewire doesnot load the file and eval the contents to emulate node's require mechanism. In fact it uses node's own require to load the module. Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications).

Debugging is fully supported.

Furthermore rewire comes also with support forbrowserify. You justhave to add rewire as a middleware (see below).

Build Status


Installation

npm install rewire

For older node versions:
rewire is tested with node 0.6.x - 0.8.x. I recommend to run the unit tests viamocha in the rewire-folder beforeusing rewire with other node versions.

Use withbrowserify:

varb=require("browserify")({debug:true});b.use(require("rewire").browserify);

After that rewire works exactly as in node.


Examples

varrewire=require("rewire");// rewire acts exactly like require.varmyModule=rewire("../lib/myModule.js");// Your module will now export a special setter and getter for private variables.myModule.__set__("myPrivateVar",123);myModule.__get__("myPrivateVar");// = 123// This allows you to mock almost everything within the module e.g. the fs-module.// Just pass the variable name as first parameter and your mock as second.myModule.__set__("fs",{readFile:function(path,encoding,cb){cb(null,"Success!");}});myModule.readSomethingFromFileSystem(function(err,data){console.log(data);// = Success!});// All later requires will now return the module with the mock.myModule===require("./myModule.js");// = true// You can set different variables with one call.myModule.__set__({fs:fsMock,http:httpMock,someOtherVar:"hello"});// You may also override globals. These changes are only within the module, so// you don't have to be concerned that other modules are influenced by your mock.myModule.__set__({console:{log:function(){/* be quiet */}},process:{argv:["testArg1","testArg2"]}});// But be careful, if you do something like this you'll change your global// console instance.myModule.__set__("console.log",function(){/* be quiet */});// By getting private variables you can test for instance if your// module is in a specific stateassert.ok(myModule.__get__("currentState")==="idle");// You can also disable caching when loading the rewired module. All// subsequent calls of require() will than return the original module again.rewire("./myModule.js")===require("./myModule.js");// = truerewire("./myModule.js",false)===require("./myModule.js");// = false// Every call of rewire returns a new instance and overwrites the old// one in the module cache.rewire("./myModule.js")===rewire("./myModule.js");// = false// If you want to remove all your rewired modules from// cache just call rewire.reset().// Do this after every single unit test to ensure a clean testing environment.rewire.reset();

##API

rewire(filename, cache): {RewiredModule}

  • {!String} filename:
    Path to the module that shall be rewired. Use it exactly like require().

  • {Boolean=true} cache (optional):
    Indicates whether the rewired module should be cached by node so subsequent calls ofrequire() willreturn the rewired module. Further calls ofrewire() will always overwrite the cache.

rewire.reset()

Removes all rewired modules fromrequire.cache. Everyrequire() will now return the original module again.

RewiredModule.__set__(name, value)

  • {!String} name:
    Name of the variable to set. The variable should be a global or defined withvar in the top-levelscope of the module.

  • {∗} value:
    The value to set

RewiredModule.__set__(env)

  • {!Object} env:
    Takes all keys as variable names and sets the values respectively.

RewiredModule.__get__(name): {∗}

Returns the private variable.


Credits

This module is inspired by the greatinjectr-module.

About

Easy monkey-patching for CodeRoad

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript99.9%
  • CoffeeScript0.1%

[8]ページ先頭

©2009-2025 Movatter.jp