fuxor
Advanced tools
Comparing version 1.0.1 to 2.0.0
60
index.js
@@ -1,34 +0,36 @@ | ||
'use strict'; | ||
const Module = require('module'); | ||
var Module = require('module'); | ||
const defaultLoad = Module._load; | ||
const mappings = new Map(); | ||
var _defaultLoad = Module._load; | ||
Module._load = function(moduleName, module) { | ||
if (moduleName.indexOf('.') === 0 && !mappings.has(moduleName)) { | ||
const path = defaultLoad('path'); | ||
const parsedPath = path.parse(module.filename); | ||
const filePath = path.resolve(process.cwd(), parsedPath.dir, moduleName); | ||
return defaultLoad(filePath); | ||
} | ||
if (mappings.has(moduleName)) { | ||
return mappings.get(moduleName); | ||
} | ||
return defaultLoad(moduleName); | ||
}; | ||
var mappings = {}; | ||
Module._load = function(moduleName) { | ||
if (moduleName.indexOf('.') === 0) { | ||
//file, only di node modules | ||
return _defaultLoad(moduleName); | ||
} | ||
if (mappings[moduleName]) { | ||
return mappings[moduleName]; | ||
} | ||
return _defaultLoad(moduleName); | ||
} | ||
module.exports = { | ||
add: function (mapping) { | ||
mappings[mapping.name] = mapping.result; | ||
}, | ||
remove: function (name) { | ||
mappings[name] = null; | ||
delete mappings[name]; | ||
}, | ||
reset: function () { | ||
Module._load = _defaultLoad; | ||
}, | ||
clear: function () { | ||
mappings = {}; | ||
add: function (mapping) { | ||
if (Array.isArray(mapping)) { | ||
mapping.forEach(map => mappings.set(map.name, map.result)); | ||
} else { | ||
mappings.set(mapping.name, mapping.result); | ||
} | ||
} | ||
}, | ||
clear: function () { | ||
mappings.clear(); | ||
}, | ||
remove: function (name) { | ||
mappings.delete(name); | ||
}, | ||
reset: function () { | ||
Module._load = defaultLoad; | ||
} | ||
} |
{ | ||
"name": "fuxor", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Simple dependency injection by overriding require", | ||
@@ -16,3 +16,4 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "NODE_ENV=test tap ./test/index.js" | ||
"test": "NODE_ENV=test tap ./test/index.js", | ||
"lint": "eslint" | ||
}, | ||
@@ -26,4 +27,8 @@ "repository": { | ||
"devDependencies": { | ||
"tap": "^0.7.1" | ||
"babel-eslint": "^7.1.1", | ||
"eslint": "^3.11.0", | ||
"eslint-config-airbnb-base": "^10.0.1", | ||
"eslint-plugin-import": "^2.2.0", | ||
"tap": "^8.0.1" | ||
} | ||
} |
@@ -7,3 +7,3 @@ [![Build Status](https://travis-ci.org/Kevnz/fuxor.png?branch=master)](https://travis-ci.org/Kevnz/fuxor) | ||
``` | ||
var fuxor = require('fuxor'); | ||
const fuxor = require('fuxor'); | ||
fuxor.add({ name: 'fs', result: { | ||
@@ -14,9 +14,50 @@ readFileSync: function () { | ||
}); | ||
const fs = require('fs'); | ||
console.log(fs.readFileSync('not really', 'anything', { whatever:'you want'}));//'Not really a file' | ||
var fs = require('fs'); | ||
``` | ||
console.log(fs.readFileSync('not really', 'anything', {whatever:'you want'}));//'Not really a file' | ||
### API | ||
#### Add | ||
``` | ||
const fuxor = require('fuxor'); | ||
// Add one entry to be overridden | ||
fuxor.add({ name: 'fs', result: { | ||
readFileSync: function () { | ||
return 'Not really a file'; | ||
} | ||
}); | ||
// Add multiple entries at once | ||
fuxor.add([{ name: 'fs', result: { | ||
readFileSync: function () { | ||
return 'Not really a file'; | ||
} | ||
}, { | ||
name: 'request', | ||
result: function () { | ||
return 'Not really a file'; | ||
} | ||
}]); | ||
``` | ||
#### Clear | ||
``` | ||
const fuxor = require('fuxor'); | ||
// After items have been added | ||
fuxor.clear(); // All entries have been removed | ||
``` | ||
This is really simple and naive and was written for my needs, but I'm happy to take pull requests | ||
#### remove | ||
``` | ||
const fuxor = require('fuxor'); | ||
// After items have been added | ||
fuxor.remove('your-module'); // The module has been removed | ||
``` | ||
#### reset | ||
``` | ||
const fuxor = require('fuxor'); | ||
// After items have been added | ||
fuxor.reset(); // require now works back to normal | ||
``` | ||
@@ -1,62 +0,142 @@ | ||
var test = require("tap").test; | ||
const test = require('tap').test; | ||
var mocking = require('../index'); | ||
test("Requiring overrides", function (t) { | ||
mocking.clear(); | ||
var fs = require('fs'); | ||
const fuxor = require('../index'); | ||
mocking.add({name:'fs', result: { | ||
test: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
t.end(); | ||
} | ||
}}) | ||
var fs2 = require('fs'); | ||
t.notEqual(fs , fs2, "should not be the same"); | ||
fs2.test(true); | ||
test('Requiring overrides', function (t) { | ||
fuxor.clear(); | ||
const fs = require('fs'); | ||
fuxor.add({name:'fs', result: { | ||
test: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
t.end(); | ||
} | ||
}}) | ||
const fs2 = require('fs'); | ||
t.notEqual(fs , fs2, 'should not be the same'); | ||
fs2.test(true); | ||
}); | ||
test("Requiring overrides multiple times", function (t) { | ||
mocking.clear(); | ||
var fs = require('fs'); | ||
mocking.add({name:'fs', result: { | ||
test: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
} | ||
}}); | ||
mocking.add({name:'mocker', result: { | ||
test: function (result) { | ||
t.ok(result, 'this should be called as well' ); | ||
t.end(); | ||
} | ||
}}); | ||
var fs2 = require('fs'); | ||
fs2.test(true); | ||
var mock = require('mocker'); | ||
mock.test(true); | ||
test('Requiring overrides multiple times', function (t) { | ||
fuxor.clear(); | ||
const fs = require('fs'); | ||
fuxor.add({name:'fs', result: { | ||
test: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
} | ||
}}); | ||
fuxor.add({name:'mocker', result: { | ||
test: function (result) { | ||
t.ok(result, 'this should be called as well' ); | ||
t.end(); | ||
} | ||
}}); | ||
const fs2 = require('fs'); | ||
fs2.test(true); | ||
const mock = require('mocker'); | ||
mock.test(true); | ||
}); | ||
test("Removing a module", function (t) { | ||
mocking.clear(); | ||
mocking.add({name:'fs', result: { | ||
mockFunction: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
} | ||
}}); | ||
var fsMock = require('fs'); | ||
fsMock.mockFunction(true); | ||
mocking.remove('fs'); | ||
var realFs = require('fs'); | ||
t.ok(realFs.mockFunction === undefined); | ||
t.end(); | ||
}); | ||
test('Removing a module', function (t) { | ||
fuxor.clear(); | ||
fuxor.add({name:'fs', result: { | ||
mockFunction: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
} | ||
}}); | ||
const fsMock = require('fs'); | ||
fsMock.mockFunction(true); | ||
fuxor.remove('fs'); | ||
const realFs = require('fs'); | ||
t.ok(realFs.mockFunction === undefined); | ||
t.end(); | ||
}); | ||
test('Clearing all modules', function (t) { | ||
fuxor.clear(); | ||
fuxor.add({ name:'fs', result: { | ||
mockFunction: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
} | ||
}}); | ||
fuxor.add({ name:'util', result: { | ||
mockedFunction: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
} | ||
}}); | ||
const fsMock = require('fs'); | ||
fsMock.mockFunction(true); | ||
const utilMock = require('util'); | ||
utilMock.mockedFunction(true); | ||
fuxor.clear(); | ||
const realFs = require('fs'); | ||
const realUtil = require('util'); | ||
t.ok(realFs.mockFunction === undefined); | ||
t.ok(realUtil.mockedFunction === undefined); | ||
t.end(); | ||
}); | ||
test('Only override modules that have been loaded', function (t) { | ||
fuxor.clear(); | ||
fuxor.add({ name:'not-really-a-module', result: { | ||
mockFunction: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
} | ||
}}); | ||
const fs = require('fs'); | ||
t.ok(fs.mockFunction === undefined); | ||
t.ok(typeof fs.readFileSync === 'function'); | ||
t.end(); | ||
}); | ||
test('Override local modules as well', function (t) { | ||
fuxor.clear(); | ||
fuxor.add({ name:'./mock', result: { | ||
mockFunction: function () { | ||
t.ok(true, 'this should be called' ); | ||
t.end(); | ||
} | ||
}}); | ||
const filemock = require('./mock'); | ||
filemock.mockFunction(); | ||
}); | ||
test('Local module not added should be called', function (t) { | ||
fuxor.clear(); | ||
fuxor.add({ name:'./mock', result: { | ||
mockFunction: function () { | ||
t.ok(true, 'this should be called' ); | ||
} | ||
}}); | ||
const nomock = require('./no-mock'); | ||
nomock(()=> { | ||
t.ok(true, 'this should be called' ); | ||
t.end(); | ||
}); | ||
}); | ||
test('Adding multiple overrides in one add', function (t) { | ||
fuxor.clear(); | ||
fuxor.add([{ name:'fs', result: { | ||
test: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
} | ||
}}, { | ||
name:'nothing', result: function (result) { | ||
t.ok(result, 'this should be called' ); | ||
} | ||
} | ||
]) | ||
const fs = require('fs'); | ||
const nothing = require('nothing'); | ||
fs.test(true); | ||
nothing(true); | ||
t.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
6738
10
164
62
5
11