Comparing version 0.1.0 to 0.2.0
@@ -1,2 +0,2 @@ | ||
var ORIGINAL = Symbol('ORIGINAL'); | ||
var PREVIOUS = Symbol('PREVIOUS'); | ||
var ON_RESTORE = Symbol('ON_RESTORE'); | ||
@@ -25,2 +25,3 @@ | ||
original = require(name); | ||
cached = require.cache[path]; | ||
} catch (err) { | ||
@@ -43,3 +44,3 @@ throw new Error('Could not modify non existing module "' + name + '"'); | ||
}; | ||
cache[ORIGINAL] = original; | ||
cache[PREVIOUS] = cached || { exports: original }; | ||
cache[ON_RESTORE] = onRestore; | ||
@@ -54,18 +55,31 @@ | ||
exports.restore = function(name) { | ||
var cached, original, path; | ||
exports.restore = function(name, steps) { | ||
steps = steps || -1; | ||
try { | ||
path = require.resolve(name); | ||
} catch (err) { | ||
throw new Error('Could not restore non existing module "' + name + '"'); | ||
} | ||
function makeRestore(count) { | ||
var cached, previous, path; | ||
if ( (cached = require.cache[path]) && (original = cached[ORIGINAL]) ) { | ||
require.cache[path] = { exports: original }; | ||
if ( (count - steps) == 0 ) { | ||
return; | ||
} | ||
cached[ON_RESTORE].forEach(function(fn) { | ||
fn.call(null); | ||
}); | ||
try { | ||
path = require.resolve(name); | ||
} catch (err) { | ||
throw new Error('Could not restore non existing module "' + name + '"'); | ||
} | ||
if ( (cached = require.cache[path]) && (previous = cached[PREVIOUS]) ) { | ||
require.cache[path] = previous; | ||
cached[ON_RESTORE].forEach(function(fn) { | ||
fn.call(null); | ||
}); | ||
// step on top | ||
makeRestore(count + 1); | ||
} | ||
} | ||
makeRestore(0); | ||
}; |
{ | ||
"name": "cuculus", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"engines" : { "node" : ">=0.12.0" }, | ||
"description": "Simplest require mocking", | ||
@@ -5,0 +6,0 @@ "author": { |
@@ -93,7 +93,11 @@ # [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] | ||
##### cuculus.modify(name: string, replacer: Function(original: Any, onRestore: Function(fn: Function))) : Function() | ||
##### cuculus.modify(name: string, replacer: Function(current: Any, onRestore: Function(fn: Function))) : Function() | ||
Modifies original module with `replacer` function. If `replacer` modifies object, then `restore` | ||
Modifies current module with `replacer` function. If `replacer` modifies object, then `restore` | ||
method will not restore the changes, until you not register the backupers with `onRestore` function. | ||
##### cuculus.restore(name: string[, steps: number]) | ||
Restores module `name`. If it was modified multiple times, restores to the root, until the `steps` limit is not given. | ||
## License | ||
@@ -100,0 +104,0 @@ |
@@ -68,13 +68,17 @@ var assert = require('assert'); | ||
it('should restore original value', function() { | ||
var stub, spy; | ||
var spy; | ||
// before | ||
stub = {}; | ||
spy = sinon.spy(); | ||
spyA = sinon.spy(); | ||
spyB = sinon.spy(); | ||
// when | ||
cuculus.modify("fs", function(original, onRestore) { | ||
onRestore(spy); | ||
return stub; | ||
cuculus.modify("fs", function(current, onRestore) { | ||
onRestore(spyA); | ||
return {}; | ||
}); | ||
cuculus.modify("fs", function(current, onRestore) { | ||
onRestore(spyB); | ||
return {}; | ||
}); | ||
cuculus.restore("fs"); | ||
@@ -84,5 +88,7 @@ | ||
expect(require("fs")).equal(fs); | ||
expect(spy.callCount).equal(1); | ||
expect(spyA.callCount).equal(1); | ||
expect(spyB.callCount).equal(1); | ||
expect(spyB.calledBefore(spyA)).to.be.true; | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7568
140
113