Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nodemock

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nodemock - npm Package Compare versions

Comparing version 0.2.13 to 0.2.14

162

lib-cov/nodemock.js

@@ -104,18 +104,17 @@ /* automatically generated by JSCoverage - do not edit */

_$jscoverage['nodemock.js'][228] = 0;
_$jscoverage['nodemock.js'][241] = 0;
_$jscoverage['nodemock.js'][242] = 0;
_$jscoverage['nodemock.js'][243] = 0;
_$jscoverage['nodemock.js'][249] = 0;
_$jscoverage['nodemock.js'][251] = 0;
_$jscoverage['nodemock.js'][255] = 0;
_$jscoverage['nodemock.js'][244] = 0;
_$jscoverage['nodemock.js'][250] = 0;
_$jscoverage['nodemock.js'][252] = 0;
_$jscoverage['nodemock.js'][256] = 0;
_$jscoverage['nodemock.js'][257] = 0;
_$jscoverage['nodemock.js'][258] = 0;
_$jscoverage['nodemock.js'][260] = 0;
_$jscoverage['nodemock.js'][263] = 0;
_$jscoverage['nodemock.js'][259] = 0;
_$jscoverage['nodemock.js'][261] = 0;
_$jscoverage['nodemock.js'][264] = 0;
_$jscoverage['nodemock.js'][267] = 0;
_$jscoverage['nodemock.js'][265] = 0;
_$jscoverage['nodemock.js'][268] = 0;
_$jscoverage['nodemock.js'][271] = 0;
_$jscoverage['nodemock.js'][273] = 0;
_$jscoverage['nodemock.js'][269] = 0;
_$jscoverage['nodemock.js'][272] = 0;
_$jscoverage['nodemock.js'][274] = 0;

@@ -127,13 +126,13 @@ _$jscoverage['nodemock.js'][275] = 0;

_$jscoverage['nodemock.js'][279] = 0;
_$jscoverage['nodemock.js'][281] = 0;
_$jscoverage['nodemock.js'][285] = 0;
_$jscoverage['nodemock.js'][280] = 0;
_$jscoverage['nodemock.js'][282] = 0;
_$jscoverage['nodemock.js'][286] = 0;
_$jscoverage['nodemock.js'][288] = 0;
_$jscoverage['nodemock.js'][290] = 0;
_$jscoverage['nodemock.js'][292] = 0;
_$jscoverage['nodemock.js'][294] = 0;
_$jscoverage['nodemock.js'][296] = 0;
_$jscoverage['nodemock.js'][298] = 0;
_$jscoverage['nodemock.js'][287] = 0;
_$jscoverage['nodemock.js'][289] = 0;
_$jscoverage['nodemock.js'][291] = 0;
_$jscoverage['nodemock.js'][293] = 0;
_$jscoverage['nodemock.js'][295] = 0;
_$jscoverage['nodemock.js'][297] = 0;
_$jscoverage['nodemock.js'][299] = 0;
_$jscoverage['nodemock.js'][301] = 0;
_$jscoverage['nodemock.js'][300] = 0;
_$jscoverage['nodemock.js'][302] = 0;

@@ -143,15 +142,16 @@ _$jscoverage['nodemock.js'][303] = 0;

_$jscoverage['nodemock.js'][305] = 0;
_$jscoverage['nodemock.js'][307] = 0;
_$jscoverage['nodemock.js'][311] = 0;
_$jscoverage['nodemock.js'][306] = 0;
_$jscoverage['nodemock.js'][308] = 0;
_$jscoverage['nodemock.js'][312] = 0;
_$jscoverage['nodemock.js'][315] = 0;
_$jscoverage['nodemock.js'][320] = 0;
_$jscoverage['nodemock.js'][322] = 0;
_$jscoverage['nodemock.js'][313] = 0;
_$jscoverage['nodemock.js'][316] = 0;
_$jscoverage['nodemock.js'][321] = 0;
_$jscoverage['nodemock.js'][323] = 0;
_$jscoverage['nodemock.js'][326] = 0;
_$jscoverage['nodemock.js'][324] = 0;
_$jscoverage['nodemock.js'][327] = 0;
_$jscoverage['nodemock.js'][330] = 0;
_$jscoverage['nodemock.js'][328] = 0;
_$jscoverage['nodemock.js'][331] = 0;
_$jscoverage['nodemock.js'][332] = 0;
_$jscoverage['nodemock.js'][333] = 0;
_$jscoverage['nodemock.js'][334] = 0;
}

@@ -407,8 +407,8 @@ _$jscoverage['nodemock.js'][27]++;

_$jscoverage['nodemock.js'][228]++;
var bypass = {"takes": true, "times": true, "returns": true, "ctrl": true, "calls": true, "assert": true, "assertThrows": true, "mock": true, "fail": true, "reset": true};
_$jscoverage['nodemock.js'][241]++;
var bypass = {"takes": true, "times": true, "returns": true, "ctrl": true, "calls": true, "assert": true, "assertThrows": true, "mock": true, "fail": true, "reset": true, "ignore": true};
_$jscoverage['nodemock.js'][242]++;
for (var key in this) {
_$jscoverage['nodemock.js'][242]++;
_$jscoverage['nodemock.js'][243]++;
if (! bypass[key]) {
_$jscoverage['nodemock.js'][243]++;
_$jscoverage['nodemock.js'][244]++;
delete this[key];

@@ -418,51 +418,51 @@ }

});
_$jscoverage['nodemock.js'][249]++;
_$jscoverage['nodemock.js'][250]++;
this.ignore = (function (method) {
_$jscoverage['nodemock.js'][251]++;
_$jscoverage['nodemock.js'][252]++;
this[method] = (function () {
});
});
_$jscoverage['nodemock.js'][255]++;
_$jscoverage['nodemock.js'][256]++;
function cloneEntry(entry) {
_$jscoverage['nodemock.js'][256]++;
_$jscoverage['nodemock.js'][257]++;
var clone = {};
_$jscoverage['nodemock.js'][257]++;
_$jscoverage['nodemock.js'][258]++;
for (var key in entry) {
_$jscoverage['nodemock.js'][258]++;
_$jscoverage['nodemock.js'][259]++;
clone[key] = entry[key];
}
_$jscoverage['nodemock.js'][260]++;
_$jscoverage['nodemock.js'][261]++;
return clone;
}
_$jscoverage['nodemock.js'][263]++;
_$jscoverage['nodemock.js'][264]++;
function getCurrentEntry() {
_$jscoverage['nodemock.js'][264]++;
_$jscoverage['nodemock.js'][265]++;
return entries[currentMockFunction][entries[currentMockFunction].length - 1];
}
_$jscoverage['nodemock.js'][267]++;
_$jscoverage['nodemock.js'][268]++;
function addEntry(entry) {
_$jscoverage['nodemock.js'][268]++;
_$jscoverage['nodemock.js'][269]++;
entries[currentMockFunction].push(entry);
}
_$jscoverage['nodemock.js'][271]++;
_$jscoverage['nodemock.js'][272]++;
function getParamString(params) {
_$jscoverage['nodemock.js'][273]++;
_$jscoverage['nodemock.js'][274]++;
var paramString = "(";
_$jscoverage['nodemock.js'][274]++;
_$jscoverage['nodemock.js'][275]++;
for (var index in params) {
_$jscoverage['nodemock.js'][275]++;
_$jscoverage['nodemock.js'][276]++;
var param = params[index];
_$jscoverage['nodemock.js'][276]++;
_$jscoverage['nodemock.js'][277]++;
if (param instanceof Function) {
_$jscoverage['nodemock.js'][277]++;
_$jscoverage['nodemock.js'][278]++;
paramString += "function() {}, ";
}
else {
_$jscoverage['nodemock.js'][278]++;
_$jscoverage['nodemock.js'][279]++;
if (param instanceof RegExp) {
_$jscoverage['nodemock.js'][279]++;
_$jscoverage['nodemock.js'][280]++;
paramString += param.toString() + ", ";
}
else {
_$jscoverage['nodemock.js'][281]++;
_$jscoverage['nodemock.js'][282]++;
paramString += JSON.stringify(param) + ", ";

@@ -472,43 +472,43 @@ }

}
_$jscoverage['nodemock.js'][285]++;
_$jscoverage['nodemock.js'][286]++;
if (params[0]) {
_$jscoverage['nodemock.js'][286]++;
_$jscoverage['nodemock.js'][287]++;
return paramString = paramString.substring(0, paramString.length - 2) + ")";
}
else {
_$jscoverage['nodemock.js'][288]++;
_$jscoverage['nodemock.js'][289]++;
return "()";
}
}
_$jscoverage['nodemock.js'][290]++;
_$jscoverage['nodemock.js'][291]++;
;
_$jscoverage['nodemock.js'][292]++;
_$jscoverage['nodemock.js'][293]++;
function deepObjectCheck(expected, actual) {
_$jscoverage['nodemock.js'][294]++;
_$jscoverage['nodemock.js'][295]++;
if (expected && actual && (expected.length != actual.length)) {
_$jscoverage['nodemock.js'][294]++;
_$jscoverage['nodemock.js'][295]++;
return false;
}
_$jscoverage['nodemock.js'][296]++;
_$jscoverage['nodemock.js'][297]++;
for (var key in expected) {
_$jscoverage['nodemock.js'][298]++;
_$jscoverage['nodemock.js'][299]++;
var actualType = typeof actual[key];
_$jscoverage['nodemock.js'][299]++;
_$jscoverage['nodemock.js'][300]++;
var expectedType = typeof expected[key];
_$jscoverage['nodemock.js'][301]++;
_$jscoverage['nodemock.js'][302]++;
if (actualType != expectedType) {
_$jscoverage['nodemock.js'][301]++;
_$jscoverage['nodemock.js'][302]++;
return false;
}
_$jscoverage['nodemock.js'][302]++;
_$jscoverage['nodemock.js'][303]++;
if (actualType == "function") {
_$jscoverage['nodemock.js'][303]++;
_$jscoverage['nodemock.js'][304]++;
continue;
}
else {
_$jscoverage['nodemock.js'][304]++;
_$jscoverage['nodemock.js'][305]++;
if (actualType == "object") {
_$jscoverage['nodemock.js'][305]++;
_$jscoverage['nodemock.js'][306]++;
if (! deepObjectCheck(expected[key], actual[key])) {
_$jscoverage['nodemock.js'][305]++;
_$jscoverage['nodemock.js'][306]++;
return false;

@@ -518,5 +518,5 @@ }

else {
_$jscoverage['nodemock.js'][307]++;
_$jscoverage['nodemock.js'][308]++;
if (actual[key] != expected[key]) {
_$jscoverage['nodemock.js'][307]++;
_$jscoverage['nodemock.js'][308]++;
return false;

@@ -527,32 +527,32 @@ }

}
_$jscoverage['nodemock.js'][311]++;
_$jscoverage['nodemock.js'][312]++;
return true;
}
_$jscoverage['nodemock.js'][312]++;
_$jscoverage['nodemock.js'][313]++;
;
_$jscoverage['nodemock.js'][315]++;
_$jscoverage['nodemock.js'][316]++;
this.mock(methodName);
}
_$jscoverage['nodemock.js'][320]++;
_$jscoverage['nodemock.js'][321]++;
if (typeof exports != "undefined") {
_$jscoverage['nodemock.js'][322]++;
_$jscoverage['nodemock.js'][323]++;
exports.mock = (function (methodName) {
_$jscoverage['nodemock.js'][323]++;
_$jscoverage['nodemock.js'][324]++;
return new NodeMock(methodName);
});
_$jscoverage['nodemock.js'][326]++;
_$jscoverage['nodemock.js'][327]++;
exports.fail = (function () {
_$jscoverage['nodemock.js'][327]++;
_$jscoverage['nodemock.js'][328]++;
return new NodeMock();
});
_$jscoverage['nodemock.js'][330]++;
_$jscoverage['nodemock.js'][331]++;
exports.ignore = (function (method) {
_$jscoverage['nodemock.js'][331]++;
_$jscoverage['nodemock.js'][332]++;
var nm = new NodeMock();
_$jscoverage['nodemock.js'][332]++;
_$jscoverage['nodemock.js'][333]++;
nm.ignore(method);
_$jscoverage['nodemock.js'][333]++;
_$jscoverage['nodemock.js'][334]++;
return nm;
});
}
_$jscoverage['nodemock.js'].source = ["/**","","\tThe MIT License","\t","\tCopyright (c) 2011 Arunoda Susiripala","\t","\tPermission is hereby granted, free of charge, to any person obtaining a copy","\tof this software and associated documentation files (the \"Software\"), to deal","\tin the Software without restriction, including without limitation the rights","\tto use, copy, modify, merge, publish, distribute, sublicense, and/or sell","\tcopies of the Software, and to permit persons to whom the Software is","\tfurnished to do so, subject to the following conditions:","\t","\tThe above copyright notice and this permission notice shall be included in","\tall copies or substantial portions of the Software.","\t","\tTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR","\tIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,","\tFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE","\tAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER","\tLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,","\tOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN","\tTHE SOFTWARE.",""," */","","function NodeMock(methodName) {","\t","\tvar self = this;","\tvar currentMockFunction = null;","\t","\tvar entries = {};","\t","\tvar mockFunction = function(method, avoid) {","\t\t","\t\tfunction getValidEntry(args) {","\t\t\t","\t\t\t//Iterate over to find a entry matching argument list","\t\t\tfor(var index in entries[method]) {","\t\t\t\tentry = entries[method][index];","\t\t","\t\t\t\tif(entry.executed == false && deepObjectCheck(entry.args, args)) {","\t\t\t\t\t//increasing executed numbers for that entry","\t\t\t\t\tentry.executed = true;","\t\t\t\t\treturn entry;","\t\t\t\t}","\t\t\t}","\t\t\t","\t\t\treturn false;","\t\t}","\t\t","\t\treturn function() {","\t\t\t//check for argument length","\t\t\tvar entry;","\t\t\t","\t\t\tif(!self[method]) {","\t\t\t\tthrow new Error(\"Mock function '\" + method + \"()' is not defined\");","\t\t\t\t","\t\t\t} else if(!(entry = getValidEntry(arguments))) {","\t\t\t\t","\t\t\t\tvar expected = \"\";","\t\t\t\tvar alreadyExecuted = false;","\t\t\t\tfor(var key in entries[method]) {","\t\t\t\t\tvar entry = entries[method][key];","\t\t\t\t\tif(entry.executed == false) {","\t\t\t\t\t\texpected += \"\\n\\t\" + getParamString(entry.args);","\t\t\t\t\t} else if(deepObjectCheck(entry.args, arguments)) {","\t\t\t\t\t\talreadyExecuted = true;","\t\t\t\t\t}","\t\t\t\t}","","\t\t\t\texpected += \"\\n\";","\t\t\t\t","\t\t\t\tif(alreadyExecuted) {","\t\t\t\t\tthrow new Error('method: ' + method + getParamString(arguments) + ' already executed');","\t\t\t\t} else {","\t\t\t\t\tthrow new Error(","\t\t\t\t\t\t\"Arguments content passed: \" + getParamString(arguments) + ","\t\t\t\t\t\t\" is not tally with expected: \" + expected + \" in method: '\" + method + \"()'\");","\t\t\t\t}","\t\t\t","\t\t\t} else if(entry.shouldFail) {","\t\t\t\tthrow new Error(\"You should not call: '\" + method+ \"()' with params: \" + getParamString(arguments) + \" on this object\");","\t\t\t}","\t\t\t","\t\t\t//calling the callback","\t\t\tif(entry.callbackIndex != null) {","\t\t\t\tvar func = arguments[entry.callbackIndex];","\t\t\t\tentry.callback = func;","\t\t\t\t","\t\t\t\tif(entry.callbackArgs) {","\t\t\t\t\tif(typeof(func) == \"function\") {","\t\t\t\t\t\tfunc.apply(this, entry.callbackArgs || []);","\t\t\t\t\t} else {","\t\t\t\t\t\tthrow new Error(\"Expected callback is not defined as callback\");","\t\t\t\t\t}","\t\t\t\t}","\t\t\t}","\t\t\t","\t\t\treturn entry.returns;","\t\t};","\t};","\t","\tthis.takes = function() {","\t\tvar entry = getCurrentEntry();","\t\tentry.args = arguments;","\t\treturn this;","\t};","\t\t","\tthis.times = function(expected) {","\t\tvar entry = getCurrentEntry();","\t\tfor(var lc = 0; lc < expected - 1; lc++) {","\t\t\taddEntry(cloneEntry(entry));","\t\t}","\t\treturn this;","\t};","\t","\tthis.returns = function(value) {","\t\tvar entry = getCurrentEntry();","\t\tentry.returns = value;","\t\treturn this;","\t};","\t","\tthis.ctrl = function(index, obj) {","\t\tvar entry = getCurrentEntry();","\t\tentry.callbackIndex = index;","\t\tentry.callbackArgs = false;","\t\t","\t\tobj.trigger = function() {","\t\t\tif(entry.callback) {","\t\t\t\tentry.callback.apply(this, arguments);","\t\t\t} else {","\t\t\t\tthrow new Error(\"There is no callback to control\");","\t\t\t}","\t\t};","\t\t","\t\treturn this;","\t};","\t","\tthis.calls = function() {","\t\t","\t\tif(typeof(arguments[0]) == \"number\") {","\t\t\tvar entry = entries[currentMockFunction][entries[currentMockFunction].length - 1];","\t\t\t","\t\t\tentry.callbackIndex = arguments[0];","\t\t\tif(arguments[1] && (arguments[1] instanceof Array)) {","\t\t\t\tentry.callbackArgs = arguments[1];","\t\t\t}","\t\t\t","\t\t\treturn this;","\t\t} else if(arguments[0] instanceof Object) {","\t\t\t","\t\t} else {","\t\t\tthrow new Error(\"First arg of the calls() should be the index of the callback\");","\t\t}","\t};","\t","\tthis.assert = function() {","\t\t","\t\tvar success = true;","\t\tfor(var method in entries) {","\t\t\tvar entriesForMethod = entries[method];","\t\t\tentriesForMethod.forEach(function(entry) {","\t\t\t\tif(entry.executed == false) {","\t\t\t\t\tsuccess = false;","\t\t\t\t\tconsole.error(","\t\t\t\t\t\t\t\"method call for: '\" + method + \"()' with params: \" + getParamString(entry.args) + \" was not executed!\\n\"","\t\t\t\t\t);","\t\t\t\t}","\t\t\t});","\t\t}","\t\t","\t\treturn success;","\t};","","\tthis.assertThrows = function() {","\t\tvar success = this.assert();","\t\tif(!success) {","\t\t\tthrow new Error('Nodemock rules breaked!');","\t\t}\t","\t};","\t","\t//Assign the mocking function","\tthis.mock = function(method) {","\t\t","\t\tif(method) {","\t\t\t//let the current mocking method be this","\t\t\tcurrentMockFunction = method;","\t\t\t","\t\t\tif(!this[method]) {","\t\t\t\t","\t\t\t\tentries[currentMockFunction] = [];","\t\t\t\t//assign the mock method","\t\t\t\tthis[method] = mockFunction(method);","\t\t\t}","\t\t\t","\t\t\taddEntry({","\t\t\t\targs: [],","\t\t\t\tcallback: null,","\t\t\t\tcallbackIndex: null,","\t\t\t\tcallbackArgs: [],","\t\t\t\treturns: undefined,","\t\t\t\texecuted: false, // whether the mock entry executed or not","\t\t\t\tshouldFail: false","\t\t\t});","\t\t}","\t\t","\t\treturn this;","\t};","\t","\t/**","\t * After this call when someone calls on this this object is'll","\t * throw an exception","\t */","\tthis.fail = function() {","\t\tvar entry = getCurrentEntry();","\t\tentry.shouldFail = true;","\t\treturn this;","\t};","","\t/**","\t\tcleanup all the rules and the mock methods","\t*/","\tthis.reset = function() {","\t\tentries = {};","\t\tcurrentMockFunction = null;","\t\t","\t\tvar bypass = {","\t\t\t'takes': true,","\t\t\t'times': true,","\t\t\t'returns': true,","\t\t\t'ctrl': true,","\t\t\t'calls': true,","\t\t\t'assert': true,","\t\t\t'assertThrows': true,","\t\t\t'mock': true,","\t\t\t'fail': true,","\t\t\t'reset': true","\t\t};","","\t\tfor(var key in this) {","\t\t\tif(!bypass[key]) {","\t\t\t\tdelete this[key];","\t\t\t}","\t\t}\t","\t};","","\t//ignore the mock","\tthis.ignore = function(method) {","\t\t","\t\tthis[method] = function() {};","\t};","","\t//method for cloning entry","\tfunction cloneEntry(entry) {","\t\tvar clone = {};","\t for(var key in entry) {","\t \tclone[key] = entry[key];","\t }","\t return clone;","\t}","\t","\tfunction getCurrentEntry() {","\t\treturn entries[currentMockFunction][entries[currentMockFunction].length - 1];","\t}","","\tfunction addEntry(entry) {","\t\tentries[currentMockFunction].push(entry);","\t}","\t","\tfunction getParamString(params) {","\t\t","\t\tvar paramString = \"(\";","\t\tfor(var index in params) {","\t\t\tvar param = params[index];","\t\t\tif(param instanceof Function) {","\t\t\t\tparamString += \"function() {}, \";","\t\t\t} else if(param instanceof RegExp) {","\t\t\t\tparamString += param.toString() + \", \";","\t\t\t} else {","\t\t\t\tparamString += JSON.stringify(param) + \", \";","\t\t\t}","\t\t}","\t\t","\t\tif(params[0]) {","\t\t\treturn paramString = paramString.substring(0, paramString.length -2) + \")\";","\t\t} else {","\t\t\treturn '()';","\t\t}","\t};","\t","\tfunction deepObjectCheck(expected, actual) {","\t\t\t","\t\tif(expected && actual && (expected.length != actual.length)) return false;","\t\t","\t\tfor(var key in expected) {","\t\t\t","\t\t\tvar actualType = typeof(actual[key]);","\t\t\tvar expectedType = typeof(expected[key]);","\t\t\t","\t\t\tif(actualType != expectedType) return false;","\t\t\tif(actualType == \"function\") {","\t\t\t\tcontinue;","\t\t\t} else if(actualType == \"object\") {","\t\t\t\tif(!deepObjectCheck(expected[key]\t,actual[key])) return false;","\t\t\t} else {","\t\t\t\tif(actual[key] != expected[key]) return false;","\t\t\t}","\t\t}","\t\t","\t\treturn true;","\t};","\t","\t//initialize the mocking","\tthis.mock(methodName);","\t","}","","//Supporting NodeJS CommonJS module system","if(typeof(exports) != \"undefined\") {","","\texports.mock = function(methodName) {","\t\treturn new NodeMock(methodName);","\t};","\t","\texports.fail = function() {","\t\treturn new NodeMock();","\t};","","\texports.ignore = function(method) {","\t\tvar nm = new NodeMock();","\t\tnm.ignore(method);","\t\treturn nm;","\t};","}"];
_$jscoverage['nodemock.js'].source = ["/**","","\tThe MIT License","\t","\tCopyright (c) 2011 Arunoda Susiripala","\t","\tPermission is hereby granted, free of charge, to any person obtaining a copy","\tof this software and associated documentation files (the \"Software\"), to deal","\tin the Software without restriction, including without limitation the rights","\tto use, copy, modify, merge, publish, distribute, sublicense, and/or sell","\tcopies of the Software, and to permit persons to whom the Software is","\tfurnished to do so, subject to the following conditions:","\t","\tThe above copyright notice and this permission notice shall be included in","\tall copies or substantial portions of the Software.","\t","\tTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR","\tIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,","\tFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE","\tAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER","\tLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,","\tOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN","\tTHE SOFTWARE.",""," */","","function NodeMock(methodName) {","\t","\tvar self = this;","\tvar currentMockFunction = null;","\t","\tvar entries = {};","\t","\tvar mockFunction = function(method, avoid) {","\t\t","\t\tfunction getValidEntry(args) {","\t\t\t","\t\t\t//Iterate over to find a entry matching argument list","\t\t\tfor(var index in entries[method]) {","\t\t\t\tentry = entries[method][index];","\t\t","\t\t\t\tif(entry.executed == false && deepObjectCheck(entry.args, args)) {","\t\t\t\t\t//increasing executed numbers for that entry","\t\t\t\t\tentry.executed = true;","\t\t\t\t\treturn entry;","\t\t\t\t}","\t\t\t}","\t\t\t","\t\t\treturn false;","\t\t}","\t\t","\t\treturn function() {","\t\t\t//check for argument length","\t\t\tvar entry;","\t\t\t","\t\t\tif(!self[method]) {","\t\t\t\tthrow new Error(\"Mock function '\" + method + \"()' is not defined\");","\t\t\t\t","\t\t\t} else if(!(entry = getValidEntry(arguments))) {","\t\t\t\t","\t\t\t\tvar expected = \"\";","\t\t\t\tvar alreadyExecuted = false;","\t\t\t\tfor(var key in entries[method]) {","\t\t\t\t\tvar entry = entries[method][key];","\t\t\t\t\tif(entry.executed == false) {","\t\t\t\t\t\texpected += \"\\n\\t\" + getParamString(entry.args);","\t\t\t\t\t} else if(deepObjectCheck(entry.args, arguments)) {","\t\t\t\t\t\talreadyExecuted = true;","\t\t\t\t\t}","\t\t\t\t}","","\t\t\t\texpected += \"\\n\";","\t\t\t\t","\t\t\t\tif(alreadyExecuted) {","\t\t\t\t\tthrow new Error('method: ' + method + getParamString(arguments) + ' already executed');","\t\t\t\t} else {","\t\t\t\t\tthrow new Error(","\t\t\t\t\t\t\"Arguments content passed: \" + getParamString(arguments) + ","\t\t\t\t\t\t\" is not tally with expected: \" + expected + \" in method: '\" + method + \"()'\");","\t\t\t\t}","\t\t\t","\t\t\t} else if(entry.shouldFail) {","\t\t\t\tthrow new Error(\"You should not call: '\" + method+ \"()' with params: \" + getParamString(arguments) + \" on this object\");","\t\t\t}","\t\t\t","\t\t\t//calling the callback","\t\t\tif(entry.callbackIndex != null) {","\t\t\t\tvar func = arguments[entry.callbackIndex];","\t\t\t\tentry.callback = func;","\t\t\t\t","\t\t\t\tif(entry.callbackArgs) {","\t\t\t\t\tif(typeof(func) == \"function\") {","\t\t\t\t\t\tfunc.apply(this, entry.callbackArgs || []);","\t\t\t\t\t} else {","\t\t\t\t\t\tthrow new Error(\"Expected callback is not defined as callback\");","\t\t\t\t\t}","\t\t\t\t}","\t\t\t}","\t\t\t","\t\t\treturn entry.returns;","\t\t};","\t};","\t","\tthis.takes = function() {","\t\tvar entry = getCurrentEntry();","\t\tentry.args = arguments;","\t\treturn this;","\t};","\t\t","\tthis.times = function(expected) {","\t\tvar entry = getCurrentEntry();","\t\tfor(var lc = 0; lc < expected - 1; lc++) {","\t\t\taddEntry(cloneEntry(entry));","\t\t}","\t\treturn this;","\t};","\t","\tthis.returns = function(value) {","\t\tvar entry = getCurrentEntry();","\t\tentry.returns = value;","\t\treturn this;","\t};","\t","\tthis.ctrl = function(index, obj) {","\t\tvar entry = getCurrentEntry();","\t\tentry.callbackIndex = index;","\t\tentry.callbackArgs = false;","\t\t","\t\tobj.trigger = function() {","\t\t\tif(entry.callback) {","\t\t\t\tentry.callback.apply(this, arguments);","\t\t\t} else {","\t\t\t\tthrow new Error(\"There is no callback to control\");","\t\t\t}","\t\t};","\t\t","\t\treturn this;","\t};","\t","\tthis.calls = function() {","\t\t","\t\tif(typeof(arguments[0]) == \"number\") {","\t\t\tvar entry = entries[currentMockFunction][entries[currentMockFunction].length - 1];","\t\t\t","\t\t\tentry.callbackIndex = arguments[0];","\t\t\tif(arguments[1] && (arguments[1] instanceof Array)) {","\t\t\t\tentry.callbackArgs = arguments[1];","\t\t\t}","\t\t\t","\t\t\treturn this;","\t\t} else if(arguments[0] instanceof Object) {","\t\t\t","\t\t} else {","\t\t\tthrow new Error(\"First arg of the calls() should be the index of the callback\");","\t\t}","\t};","\t","\tthis.assert = function() {","\t\t","\t\tvar success = true;","\t\tfor(var method in entries) {","\t\t\tvar entriesForMethod = entries[method];","\t\t\tentriesForMethod.forEach(function(entry) {","\t\t\t\tif(entry.executed == false) {","\t\t\t\t\tsuccess = false;","\t\t\t\t\tconsole.error(","\t\t\t\t\t\t\t\"method call for: '\" + method + \"()' with params: \" + getParamString(entry.args) + \" was not executed!\\n\"","\t\t\t\t\t);","\t\t\t\t}","\t\t\t});","\t\t}","\t\t","\t\treturn success;","\t};","","\tthis.assertThrows = function() {","\t\tvar success = this.assert();","\t\tif(!success) {","\t\t\tthrow new Error('Nodemock rules breaked!');","\t\t}\t","\t};","\t","\t//Assign the mocking function","\tthis.mock = function(method) {","\t\t","\t\tif(method) {","\t\t\t//let the current mocking method be this","\t\t\tcurrentMockFunction = method;","\t\t\t","\t\t\tif(!this[method]) {","\t\t\t\t","\t\t\t\tentries[currentMockFunction] = [];","\t\t\t\t//assign the mock method","\t\t\t\tthis[method] = mockFunction(method);","\t\t\t}","\t\t\t","\t\t\taddEntry({","\t\t\t\targs: [],","\t\t\t\tcallback: null,","\t\t\t\tcallbackIndex: null,","\t\t\t\tcallbackArgs: [],","\t\t\t\treturns: undefined,","\t\t\t\texecuted: false, // whether the mock entry executed or not","\t\t\t\tshouldFail: false","\t\t\t});","\t\t}","\t\t","\t\treturn this;","\t};","\t","\t/**","\t * After this call when someone calls on this this object is'll","\t * throw an exception","\t */","\tthis.fail = function() {","\t\tvar entry = getCurrentEntry();","\t\tentry.shouldFail = true;","\t\treturn this;","\t};","","\t/**","\t\tcleanup all the rules and the mock methods","\t*/","\tthis.reset = function() {","\t\tentries = {};","\t\tcurrentMockFunction = null;","\t\t","\t\tvar bypass = {","\t\t\t'takes': true,","\t\t\t'times': true,","\t\t\t'returns': true,","\t\t\t'ctrl': true,","\t\t\t'calls': true,","\t\t\t'assert': true,","\t\t\t'assertThrows': true,","\t\t\t'mock': true,","\t\t\t'fail': true,","\t\t\t'reset': true,","\t\t\t'ignore': true","\t\t};","","\t\tfor(var key in this) {","\t\t\tif(!bypass[key]) {","\t\t\t\tdelete this[key];","\t\t\t}","\t\t}\t","\t};","","\t//ignore the mock","\tthis.ignore = function(method) {","\t\t","\t\tthis[method] = function() {};","\t};","","\t//method for cloning entry","\tfunction cloneEntry(entry) {","\t\tvar clone = {};","\t for(var key in entry) {","\t \tclone[key] = entry[key];","\t }","\t return clone;","\t}","\t","\tfunction getCurrentEntry() {","\t\treturn entries[currentMockFunction][entries[currentMockFunction].length - 1];","\t}","","\tfunction addEntry(entry) {","\t\tentries[currentMockFunction].push(entry);","\t}","\t","\tfunction getParamString(params) {","\t\t","\t\tvar paramString = \"(\";","\t\tfor(var index in params) {","\t\t\tvar param = params[index];","\t\t\tif(param instanceof Function) {","\t\t\t\tparamString += \"function() {}, \";","\t\t\t} else if(param instanceof RegExp) {","\t\t\t\tparamString += param.toString() + \", \";","\t\t\t} else {","\t\t\t\tparamString += JSON.stringify(param) + \", \";","\t\t\t}","\t\t}","\t\t","\t\tif(params[0]) {","\t\t\treturn paramString = paramString.substring(0, paramString.length -2) + \")\";","\t\t} else {","\t\t\treturn '()';","\t\t}","\t};","\t","\tfunction deepObjectCheck(expected, actual) {","\t\t\t","\t\tif(expected && actual && (expected.length != actual.length)) return false;","\t\t","\t\tfor(var key in expected) {","\t\t\t","\t\t\tvar actualType = typeof(actual[key]);","\t\t\tvar expectedType = typeof(expected[key]);","\t\t\t","\t\t\tif(actualType != expectedType) return false;","\t\t\tif(actualType == \"function\") {","\t\t\t\tcontinue;","\t\t\t} else if(actualType == \"object\") {","\t\t\t\tif(!deepObjectCheck(expected[key]\t,actual[key])) return false;","\t\t\t} else {","\t\t\t\tif(actual[key] != expected[key]) return false;","\t\t\t}","\t\t}","\t\t","\t\treturn true;","\t};","\t","\t//initialize the mocking","\tthis.mock(methodName);","\t","}","","//Supporting NodeJS CommonJS module system","if(typeof(exports) != \"undefined\") {","","\texports.mock = function(methodName) {","\t\treturn new NodeMock(methodName);","\t};","\t","\texports.fail = function() {","\t\treturn new NodeMock();","\t};","","\texports.ignore = function(method) {","\t\tvar nm = new NodeMock();","\t\tnm.ignore(method);","\t\treturn nm;","\t};","}"];

@@ -238,3 +238,4 @@ /**

'fail': true,
'reset': true
'reset': true,
'ignore': true
};

@@ -241,0 +242,0 @@

{
"name": "nodemock",
"version": "0.2.13",
"version": "0.2.14",
"directories": {

@@ -5,0 +5,0 @@ "lib": "./lib"

@@ -23,2 +23,3 @@ Node Mock - Simple Yet Powerful Mocking Framework for NodeJs

* Repetitive support
* ignore methods from mocking behaviour

@@ -135,4 +136,13 @@ Testing

mock.assert() //returns true
### ignore method
Sometime we need to ignore some methods going through mocking rules. But we need to have those methods but doing nothing.
var mocked = mock.ignore('hello');
mocked.mock('foo').returns(100);
mock.foo(); //returns 100
mock.hello(); //do nothing but the method exists
mock.assert(); // return true, assert have nothing to do with ignored methods
API Documentation

@@ -175,3 +185,7 @@ -----------------

mocked.reset()
Reset all the rules and mocks created. And bring mocked object into a stage when it's created
Reset all the rules and mocks created. And bring mocked object into a stage when
it's created
mocked.ignore()
Ignore Some methods from the mocking behaviour

@@ -178,0 +192,0 @@ ### Confirm ###

@@ -444,2 +444,18 @@ /*

};
exports.testIgnoreAfterReset = function(test) {
var mock = nm.mock('foo').returns(10);
mock.reset();
mock.ignore('hello');
test.ok(mock.hello);
test.doesNotThrow(function() {
mock.hello();
});
test.done();
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc