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

Commit

Permalink
Account for null parent, run in strict mode, use mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
boblauer committed Jan 12, 2018
1 parent 2a2cd27 commit 32d0b4f
Show file tree
Hide file tree
Showing 15 changed files with 1,272 additions and 212 deletions.
211 changes: 211 additions & 0 deletions .eslintrc
@@ -0,0 +1,211 @@
{
"env": {
"node": true,
"mocha": true,
"es6": true
},
"rules": {
"array-bracket-spacing": [
"error",
"never"
],
"arrow-body-style": "off",
"arrow-parens": [
"error",
"as-needed",
{
"requireForBlockBody": true
}
],
"arrow-spacing": [
"error",
{
"before": true,
"after": true
}
],
"brace-style": [
"error",
"1tbs",
{
"allowSingleLine": true
}
],
"camelcase": "error",
"comma-spacing": [
"error",
{
"before": false,
"after": true
}
],
"comma-style": [
"error",
"last"
],
"curly": [
"error",
"multi-line"
],
"dot-notation": [
"error",
{
"allowKeywords": true
}
],
"eqeqeq": [
"error",
"allow-null"
],
"generator-star-spacing": [
"error",
{
"before": false,
"after": true
}
],
"guard-for-in": "off",
"handle-callback-err": [
"error",
"err"
],
"indent": [
"error",
2,
{
"MemberExpression": 1,
"SwitchCase": 1
}
],
"key-spacing": [
"error",
{
"beforeColon": false,
"afterColon": true,
"mode": "strict"
}
],
"keyword-spacing": [
"error",
{
"after": true
}
],
"new-cap": "error",
"no-array-constructor": "error",
"no-bitwise": "error",
"no-caller": "error",
"no-cond-assign": [
"error",
"except-parens"
],
"no-confusing-arrow": [
"error",
{
"allowParens": true
}
],
"no-console": [
"error",
{
"allow": [
"error"
]
}
],
"no-const-assign": "error",
"no-debugger": "error",
"no-empty": "error",
"no-eval": "error",
"no-extend-native": "error",
"no-implicit-coercion": "error",
"no-irregular-whitespace": "error",
"no-iterator": "error",
"no-loop-func": "error",
"no-mixed-spaces-and-tabs": "error",
"no-multiple-empty-lines": [
"error",
{
"max": 2
}
],
"no-multi-spaces": "error",
"no-multi-str": "error",
"no-new": "error",
"no-new-object": "error",
"no-proto": "error",
"no-script-url": "error",
"no-sequences": "error",
"no-shadow": "off",
"no-trailing-spaces": "error",
"no-undef": "error",
"no-unneeded-ternary": "error",
"no-unused-vars": [
"error",
{
"vars": "all",
"args": "none"
}
],
"no-var": "error",
"no-whitespace-before-property": "error",
"no-with": "error",
"object-curly-spacing": [
"error",
"always"
],
"one-var": [
"error",
"never"
],
"padded-blocks": [
"error",
"never"
],
"prefer-arrow-callback": [
"error",
{
"allowNamedFunctions": true
}
],
"prefer-const": "error",
"quotes": [
"error",
"single",
{
"allowTemplateLiterals": true,
"avoidEscape": true
}
],
"semi": [
"error",
"always"
],
"space-before-blocks": "error",
"space-before-function-paren": [
"error",
{
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}
],
"space-infix-ops": "error",
"space-unary-ops": [
"error",
{
"words": true,
"nonwords": false
}
],
"strict": [
"error",
"global"
],
"valid-typeof": "error",
"wrap-iife": [
"error",
"inside"
]
}
}
65 changes: 35 additions & 30 deletions index.js
@@ -1,20 +1,24 @@
var Module = require('module')
, dirname = require('path').dirname
, join = require('path').join
, resolve = require('path').resolve
, callerId = require('caller-id')
, originalLoader = Module._load
, mockExports = {}
, pendingMockExports = {}
;
'use strict';

const Module = require('module');
const dirname = require('path').dirname;
const join = require('path').join;
const resolve = require('path').resolve;
const getCallerFile = require('get-caller-file');
const originalLoader = Module._load;

let mockExports = {};
let pendingMockExports = {};

Module._load = function(request, parent) {
var fullFilePath = getFullPath(request, parent.filename);
if (!parent) return originalLoader.apply(this, arguments);

const fullFilePath = getFullPath(request, parent.filename);

if (pendingMockExports.hasOwnProperty(fullFilePath)){
if (pendingMockExports.hasOwnProperty(fullFilePath)) {
mockExports[fullFilePath] = typeof pendingMockExports[fullFilePath] === 'string' ?
require(pendingMockExports[fullFilePath]) :
pendingMockExports[fullFilePath];
require(pendingMockExports[fullFilePath]) :
pendingMockExports[fullFilePath];

delete pendingMockExports[fullFilePath];
}
Expand All @@ -25,7 +29,7 @@ Module._load = function(request, parent) {
};

function startMocking(path, mockExport) {
var calledFrom = callerId.getData().filePath;
const calledFrom = getCallerFile();

if (typeof mockExport === 'string') {
mockExport = getFullPath(mockExport, calledFrom);
Expand All @@ -35,8 +39,8 @@ function startMocking(path, mockExport) {
}

function stopMocking(path) {
var calledFrom = callerId.getData().filePath;
var fullPath = getFullPath(path, calledFrom);
const calledFrom = getCallerFile();
const fullPath = getFullPath(path, calledFrom);
delete pendingMockExports[fullPath];
delete mockExports[fullPath];
}
Expand All @@ -47,7 +51,7 @@ function stopMockingAll() {
}

function reRequire(path) {
var module = getFullPath(path, callerId.getData().filePath);
const module = getFullPath(path, getCallerFile());
delete require.cache[require.resolve(module)];
return require(module);
}
Expand All @@ -56,24 +60,26 @@ function isInNodePath(resolvedPath) {
if (!resolvedPath) return false;

return Module.globalPaths
.map(function(nodePath) {
.map((nodePath) => {
return resolve(process.cwd(), nodePath) + '/';
})
.some(function(fullNodePath) {
.some((fullNodePath) => {
return resolvedPath.indexOf(fullNodePath) === 0;
});
}

function getFullPath(path, calledFrom) {
var resolvedPath;
let resolvedPath;
try {
resolvedPath = require.resolve(path);
} catch(e) { }
} catch (e) {
// do nothing
}

var isLocalModule = /^\.{1,2}[/\\]?/.test(path);
var isInPath = isInNodePath(resolvedPath);
var isExternal = !isLocalModule && /[/\\]node_modules[/\\]/.test(resolvedPath);
var isSystemModule = resolvedPath === path;
const isLocalModule = /^\.{1,2}[/\\]?/.test(path);
const isInPath = isInNodePath(resolvedPath);
const isExternal = !isLocalModule && /[/\\]node_modules[/\\]/.test(resolvedPath);
const isSystemModule = resolvedPath === path;

if (isExternal || isSystemModule || isInPath) {
return resolvedPath;
Expand All @@ -83,17 +89,16 @@ function getFullPath(path, calledFrom) {
return path;
}

var localModuleName = join(dirname(calledFrom), path);
const localModuleName = join(dirname(calledFrom), path);
try {
return Module._resolveFilename(localModuleName);
} catch (e) {
if (isModuleNotFoundError(e)) { return localModuleName; }
else { throw e; }
if (isModuleNotFoundError(e)) { return localModuleName; } else { throw e; }
}
}

function isModuleNotFoundError(e){
return e.code && e.code === 'MODULE_NOT_FOUND'
function isModuleNotFoundError(e) {
return e.code && e.code === 'MODULE_NOT_FOUND';
}

module.exports = startMocking;
Expand Down
11 changes: 8 additions & 3 deletions package.json
Expand Up @@ -7,11 +7,16 @@
"test": "test"
},
"dependencies": {
"caller-id": "^0.1.0"
"caller-id": "^0.1.0",
"get-caller-file": "^1.0.2"
},
"devDependencies": {
"eslint": "^4.15.0",
"mocha": "^4.1.0"
},
"devDependencies": {},
"scripts": {
"test": "NODE_PATH=test/node-path node test/runner"
"lint": "eslint .",
"test": "NODE_PATH=test/node-path mocha ./test/runner"
},
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions test/exported-fn.js
@@ -1,3 +1,5 @@
'use strict';

module.exports = function() {
return 'exported function';
};
4 changes: 3 additions & 1 deletion test/exported-obj.js
@@ -1,6 +1,8 @@
'use strict';

module.exports = {
mocked: false,
fn: function() {
fn() {
return 'exported object';
}
};
2 changes: 2 additions & 0 deletions test/index.js
@@ -1 +1,3 @@
'use strict';

module.exports = 'root';
4 changes: 3 additions & 1 deletion test/module-a.js
@@ -1,4 +1,6 @@
var a = require('module-a')
'use strict';

const a = require('module-a');

module.exports = {
id: 'local-module-a',
Expand Down
4 changes: 3 additions & 1 deletion test/module-b.js
@@ -1,4 +1,6 @@
var a = require('./module-a')
'use strict';

const a = require('./module-a');

module.exports = {
id: 'local-module-b',
Expand Down

0 comments on commit 32d0b4f

Please sign in to comment.