Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Fix: reRequiring files with naming collisions with mock-require #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
test/node-path/index.js
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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');
Expand Down