fuxor
Advanced tools
Comparing version 2.1.1 to 2.2.1
10
index.js
@@ -0,1 +1,2 @@ | ||
'use strict'; | ||
const Module = require('module'); | ||
@@ -5,4 +6,9 @@ | ||
const mappings = new Map(); | ||
let wrapper; | ||
Module._load = function(moduleName, module) { | ||
if (wrapper) { | ||
const loadedModule = defaultLoad(moduleName, module); | ||
return wrapper(loadedModule); | ||
} | ||
if (moduleName.indexOf('.') === 0 && !mappings.has(moduleName)) { | ||
@@ -32,2 +38,3 @@ const path = defaultLoad('path'); | ||
mappings.clear(); | ||
wrapper = null; | ||
}, | ||
@@ -39,3 +46,6 @@ remove: function (name) { | ||
Module._load = defaultLoad; | ||
}, | ||
wrap: function(wrapFunction) { | ||
wrapper = wrapFunction; | ||
} | ||
} |
{ | ||
"name": "fuxor", | ||
"version": "2.1.1", | ||
"version": "2.2.1", | ||
"description": "Simple dependency injection by overriding require", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -158,1 +158,22 @@ const test = require('tap').test; | ||
}); | ||
test('wrap a module', (t) => { | ||
fuxor.clear(); | ||
fuxor.wrap((module) => { | ||
t.ok(module, 'this should be called' ); | ||
t.end(); | ||
}); | ||
const wrappedResult = require('util'); | ||
console.log(wrappedResult); | ||
}); | ||
test('wrap multiple modules', (t) => { | ||
t.plan(2) | ||
fuxor.clear(); | ||
fuxor.wrap((module) => { | ||
t.ok(module, 'this should be called' ); | ||
}); | ||
const wrappedResult = require('util'); | ||
const wrappedFS = require('fs'); | ||
t.end(); | ||
}); |
8014
208
14