mock-require
Advanced tools
Comparing version 2.0.1 to 2.0.2
16
index.js
var Module = require('module') | ||
, dirname = require('path').dirname | ||
, join = require('path').join | ||
, resolve = require('path').resolve | ||
, callerId = require('caller-id') | ||
@@ -54,2 +55,14 @@ , originalLoader = Module._load | ||
function isInNodePath(resolvedPath) { | ||
if (!resolvedPath) return false; | ||
return Module.globalPaths | ||
.map(function(nodePath) { | ||
return resolve(process.cwd(), nodePath) + '/'; | ||
}) | ||
.some(function(fullNodePath) { | ||
return resolvedPath.indexOf(fullNodePath) === 0; | ||
}); | ||
} | ||
function getFullPath(path, calledFrom) { | ||
@@ -62,6 +75,7 @@ var resolvedPath; | ||
var isLocalModule = /^\.{1,2}[/\\]?/.test(path); | ||
var isInPath = isInNodePath(resolvedPath); | ||
var isExternal = !isLocalModule && /[/\\]node_modules[/\\]/.test(resolvedPath); | ||
var isSystemModule = resolvedPath === path; | ||
if (isExternal || isSystemModule) { | ||
if (isExternal || isSystemModule || isInPath) { | ||
return resolvedPath; | ||
@@ -68,0 +82,0 @@ } |
{ | ||
"name": "mock-require", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Simple, intuitive mocking of Node.js modules.", | ||
@@ -14,3 +14,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "node test/runner" | ||
"test": "NODE_PATH=test/node-path node test/runner" | ||
}, | ||
@@ -17,0 +17,0 @@ "repository": { |
@@ -211,2 +211,16 @@ var assert = require('assert') | ||
(function shouldMockFilesInNodePathByFullPath() { | ||
mock('in-node-path', {id: 'in-node-path'}); | ||
var b = require('in-node-path') | ||
var c = require('./node-path/in-node-path'); | ||
assert.equal(b.id, 'in-node-path'); | ||
assert.equal(c.id, 'in-node-path'); | ||
assert.equal(b, c); | ||
mock.stopAll(); | ||
})(); | ||
console.log('All tests pass!'); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14770
18
280
0