Repository URL to install this package:
|
Version:
1.8.0-17108 ▾
|
{
"name": "rewire",
"version": "1.1.2",
"description": "Dependency injection for node.js applications",
"keywords": [
"dependency",
"injection",
"mock",
"shim",
"module",
"unit",
"test",
"leak",
"inspect"
],
"author": {
"name": "Johannes Ewald",
"email": "mail@johannesewald.de"
},
"main": "lib/index.js",
"homepage": "https://github.com/jhnns/rewire",
"bugs": {
"url": "https://github.com/jhnns/rewire/issues",
"email": "mail@johannesewald.de"
},
"repository": {
"type": "git",
"url": "git://github.com/jhnns/rewire.git"
},
"engines": {
"node": "<0.12.x"
},
"devDependencies": {
"mocha": "1.x",
"expect.js": "0.x",
"coffee-script": "1.x"
},
"scripts": {
"test": "node node_modules/mocha/bin/mocha -R spec"
},
"readme": "rewire\n=====\n**Dependency injection for node.js applications**.\n\nrewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may\n\n- inject mocks for other modules or globals like `process`\n- leak private variables\n- override variables within the module.\n\nrewire does **not** 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).\n\nGood news to all caffeine-addicts: rewire works also with [Coffee-Script](http://coffeescript.org/). Note that in this case CS needs to be listed in your devDependencies.\n\nIf you want to use rewire also on the client-side take a look at [client-side bundlers](https://github.com/jhnns/rewire#client-side-bundlers)\n\n[](http://travis-ci.org/jhnns/rewire)\n[](http://david-dm.org/jhnns/rewire)\nDependency tracking by [David](http://david-dm.org/)\n\n<br />\n\nInstallation\n------------\n\n`npm install rewire`\n\n<br />\n\nExamples\n--------\n\nImagine you want to test this module:\n\n```javascript\n// lib/myModule.js\n\n// With rewire you can change all these variables\nvar fs = require(\"fs\"),\n http = require(\"http\"),\n someOtherVar = \"hi\",\n myPrivateVar = 1;\n\nfunction readSomethingFromFileSystem(cb) {\n // But no scoped variables\n var path = \"/somewhere/on/the/disk\";\n\n console.log(\"Reading from file system ...\");\n fs.readFile(path, \"utf8\", cb);\n}\n\nexports.readSomethingFromFileSystem = readSomethingFromFileSystem;\n```\n\nNow within your test module:\n\n```javascript\n// test/myModule.test.js\n\nvar rewire = require(\"rewire\");\n\n// rewire acts exactly like require.\nvar myModule = rewire(\"../lib/myModule.js\");\n\n// Just with one difference:\n// Your module will now export a special setter and getter for private variables.\nmyModule.__set__(\"myPrivateVar\", 123);\nmyModule.__get__(\"myPrivateVar\"); // = 123\n\n// This allows you to mock almost everything within the module e.g. the fs-module.\n// Just pass the variable name as first parameter and your mock as second.\nmyModule.__set__(\"fs\", {\n readFile: function (path, encoding, cb) {\n cb(null, \"Success!\");\n }\n});\nmyModule.readSomethingFromFileSystem(function (err, data) {\n console.log(data); // = Success!\n});\n\n// You can set different variables with one call.\nmyModule.__set__({\n fs: fsMock,\n http: httpMock,\n someOtherVar: \"hello\"\n});\n\n// You may also override globals. These changes are only within the module, so\n// you don't have to be concerned that other modules are influenced by your mock.\nmyModule.__set__({\n console: {\n log: function () { /* be quiet */ }\n },\n process: {\n argv: [\"testArg1\", \"testArg2\"]\n }\n});\n\n// But be careful, if you do something like this you'll change your global\n// console instance.\nmyModule.__set__(\"console.log\", function () { /* be quiet */ });\n\n// There is another difference to require:\n// Every call of rewire() returns a new instance.\nrewire(\"./myModule.js\") === rewire(\"./myModule.js\"); // = false\n```\n\n<br />\n\n##API\n\n###rewire(filename): rewiredModule\n\n- *filename*: <br/>\nPath to the module that shall be rewired. Use it exactly like require().\n\n###rewiredModule.__set__(name, value)\n\n- *name*: <br/>\nName of the variable to set. The variable should be global or defined with `var` in the top-leve scope of the module.\n- *value*: <br/>\nThe value to set.\n\n###rewiredModule.__set__(env)\n- *env*: <br/>\nTakes all keys as variable names and sets the values respectively.\n\n###rewiredModule.__get__(name): value\n\nReturns the private variable.\n\n<br />\n\n##Client-Side Bundlers\n\n###webpack\nSee [rewire-webpack](https://github.com/jhnns/rewire-webpack)\n\n###browserify\nrewire currently only supports browserify@1.x. I'm not planing to continue development, but if you're relying on this feature [please let me know](https://github.com/jhnns/rewire/issues/13).\n\n<br />\n\n##License\n\nMIT\n",
"readmeFilename": "README.md",
"_id": "rewire@1.1.2",
"_from": "rewire@"
}