Permalink
Browse files

New clean cache option.

  • Loading branch information...
1 parent 6c38798 commit f204aca2531d22c8dfa96c29bc20ddbb60ed9785 @mfncooper committed Dec 29, 2012
Showing with 27 additions and 0 deletions.
  1. +27 −0 mockery.js
View
@@ -41,7 +41,9 @@ var m = require('module'),
registeredSubstitutes = {},
registeredAllowables = {},
originalLoader = null,
+ originalCache = null,
defaultOptions = {
+ useCleanCache: false,
warnOnReplace: true,
warnOnUnregistered: true
},
@@ -136,7 +138,14 @@ function enable(opts) {
// Already hooked
return;
}
+
options = getEffectiveOptions(opts);
+
+ if (options.useCleanCache) {
+ originalCache = m._cache;
+ m._cache = {};
+ }
+
originalLoader = m._load;
m._load = hookedLoader;
}
@@ -151,10 +160,27 @@ function disable() {
// Not hooked
return;
}
+
+ if (options.useCleanCache) {
+ m._cache = originalCache;
+ originalCache = null;
+ }
+
m._load = originalLoader;
originalLoader = null;
}
+ /*
+ * If the clean cache option is in effect, reset the module cache to an empty
+ * state. Calling this function when the clean cache option is not in effect
+ * will have no ill effects, but will do nothing.
+ */
+function resetCache() {
+ if (options.useCleanCache && originalCache) {
+ m._cache = {};
+ }
+}
+
/*
* Enable or disable warnings to the console when previously registered mocks
* and subsitutes are replaced.
@@ -299,6 +325,7 @@ function deregisterAll() {
// Exported functions
exports.enable = enable;
exports.disable = disable;
+exports.resetCache = resetCache;
exports.warnOnReplace = warnOnReplace;
exports.warnOnUnregistered = warnOnUnregistered;
exports.registerMock = registerMock;

0 comments on commit f204aca

Please sign in to comment.