Skip to content

Commit

Permalink
Increase performance for resetting cache with native modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Murphy committed Apr 13, 2016
1 parent 48b23aa commit a893968
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 15 additions & 11 deletions mockery.js
Expand Up @@ -108,17 +108,6 @@ function hookedLoader(request, parent, isMain) {
}
}

// Starting in node 0.12 node won't reload native modules
// The reason is that native modules can register themselves to be loaded automatically
// This will re-populate the cache with the native modules that have not been mocked
if(originalCache) {
Object.keys(originalCache).forEach(function(k) {
if (k.indexOf('\.node') > -1 && !m._cache[k]) {
m._cache[k] = originalCache[k];
}
});
}

return originalLoader(request, parent, isMain);
}

Expand All @@ -138,6 +127,7 @@ function enable(opts) {
if (options.useCleanCache) {
originalCache = m._cache;
m._cache = {};
repopulateNative();
}

originalLoader = m._load;
Expand Down Expand Up @@ -172,9 +162,23 @@ function disable() {
function resetCache() {
if (options.useCleanCache && originalCache) {
m._cache = {};
repopulateNative();
}
}

/*
* Starting in node 0.12 node won't reload native modules
* The reason is that native modules can register themselves to be loaded automatically
* This will re-populate the cache with the native modules that have not been mocked
*/
function repopulateNative() {
Object.keys(originalCache).forEach(function(k) {
if (k.indexOf('\.node') > -1 && !m._cache[k]) {
m._cache[k] = originalCache[k];
}
});
}

/*
* Enable or disable warnings to the console when previously registered mocks
* and subsitutes are replaced.
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "mockery",
"version": "1.5.1",
"version": "1.6.1",
"description": "Simplifying the use of mocks with Node.js",
"keywords": [
"mock",
Expand Down

0 comments on commit a893968

Please sign in to comment.