diff --git a/.gitignore b/.gitignore index 3c3629e..e5409ad 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +test/node-path/index.js diff --git a/index.js b/index.js index cc514a8..bebad76 100644 --- a/index.js +++ b/index.js @@ -72,7 +72,7 @@ function isInNodePath(resolvedPath) { function getFullPath(path, calledFrom) { let resolvedPath; try { - resolvedPath = require.resolve(path); + resolvedPath = Module._resolveFilename(path, Module._cache[calledFrom]); } catch (e) { // do nothing } diff --git a/package.json b/package.json index b79ed50..3e9ef2d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ }, "scripts": { "lint": "eslint .", - "test": "NODE_PATH=test/node-path mocha ./test/runner" + "test": "cp index.js test/node-path/index.js && NODE_PATH=test/node-path mocha ./test/runner && rm test/node-path/index.js" }, "repository": { "type": "git", diff --git a/test/runner.js b/test/runner.js index 53fd1d1..caa4728 100644 --- a/test/runner.js +++ b/test/runner.js @@ -2,7 +2,7 @@ const assert = require('assert'); const normalize = require('normalize-path'); -const mock = require('..'); +const mock = require('./node-path/index'); describe('Mock Require', () => { afterEach(() => { @@ -78,6 +78,10 @@ describe('Mock Require', () => { assert.equal(mock.reRequire('.'), 'root'); }); + it('should support re-requiring names with collisions', () => { + assert.equal(mock.reRequire('./index'), 'root') + }) + it('should cascade mocks', () => { mock('path', { mocked: true }); mock('fs', 'path');