Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First version

  • Loading branch information...
commit e7766d6f8bcff30b0ca81d80a6f3f171a67f9adf 0 parents
Scott McWhirter authored
Showing with 105 additions and 0 deletions.
  1. +22 −0 LICENSE
  2. +32 −0 README.md
  3. +35 −0 lib/index.js
  4. +16 −0 package.json
22 LICENSE
@@ -0,0 +1,22 @@
+# This is the MIT license
+
+Copyright (c) 2012 - Scott McWhirter
+
+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.
32 README.md
@@ -0,0 +1,32 @@
+
+Mocked is a small library that allows you to intercept calls to require() and
+load the specified libaries from a directory of your choosing.
+
+# Installation
+
+ npm install mocked
+
+# Usage
+
+The usage is as so:
+
+ var mocked = require('mocked');
+
+ // Use the directory ./tests/mocks and list the libs to be mocked
+ mocked('./tests/mocks', ['http', 'net', './some/dependency']);
+
+ // this will be at ./tests/mocks/deps/logger.js
+ var logger = require('./deps/logger');
+
+# Github
+
+For git repository and issues, please see [](http://github.com/konobi/node-mocked)
+
+# License
+
+MIT. (See LICENSE file)
+
+# Author
+
+Scott McWhirter - [Github profile](http://github.com/konobi)
+
35 lib/index.js
@@ -0,0 +1,35 @@
+var p = require('path');
+
+module = module.exports = function (mock_path, libs){
+
+ if(mock_path == '')
+ throw new Error("Mock path must be a string");
+
+ // Strip any trailing slashs so that we have a somewhat normalized
+ // path to work from
+ mock_path = mock_path.replace(/\/+$/, '');
+
+ if(!(libs instanceof Array))
+ throw new Error("Mocked libs must be an array");
+
+ var Module = require('module');
+ var orig_req = Module.prototype.require;
+
+ // Here we override the prototype for require because each package
+ // will get a new module object.
+ Module.prototype.require = function(path){
+ var self = this;
+ var new_path = path;
+
+ var foo = libs.filter(function(libname){
+ // XXX - this check may not be sufficient =0(
+ return path.match(new RegExp(libname+'$'));
+ });
+
+ if(foo.length > 0)
+ new_path = mock_path + p.normalize('/' + path);
+
+ return orig_req.apply(self, [new_path]);
+ }
+}
+
16 package.json
@@ -0,0 +1,16 @@
+{
+ "name": "mocked",
+ "description": "A small library to enable lightweight mocking for tests",
+ "version": "0.0.1",
+ "main": "lib/index.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "author": "Scott McWhirter (http://github.com/konobi)",
+ "keywords": ["mock", "mocking", "test", "unittest"],
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/konobi/node-mocked.git"
+ },
+ "license": "MIT"
+}

1 comment on commit e7766d6

@konobi
Owner

@vojtajina how'd you feel about this library in comparison to your suggested method?

Please sign in to comment.
Something went wrong with that request. Please try again.