Socket
Socket
Sign inDemoInstall

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.12 to 0.2.13

153

lib-cov/nodemock.js

@@ -108,23 +108,22 @@ /* automatically generated by JSCoverage - do not edit */

_$jscoverage['nodemock.js'][249] = 0;
_$jscoverage['nodemock.js'][250] = 0;
_$jscoverage['nodemock.js'][251] = 0;
_$jscoverage['nodemock.js'][252] = 0;
_$jscoverage['nodemock.js'][254] = 0;
_$jscoverage['nodemock.js'][255] = 0;
_$jscoverage['nodemock.js'][256] = 0;
_$jscoverage['nodemock.js'][257] = 0;
_$jscoverage['nodemock.js'][258] = 0;
_$jscoverage['nodemock.js'][261] = 0;
_$jscoverage['nodemock.js'][262] = 0;
_$jscoverage['nodemock.js'][265] = 0;
_$jscoverage['nodemock.js'][260] = 0;
_$jscoverage['nodemock.js'][263] = 0;
_$jscoverage['nodemock.js'][264] = 0;
_$jscoverage['nodemock.js'][267] = 0;
_$jscoverage['nodemock.js'][268] = 0;
_$jscoverage['nodemock.js'][269] = 0;
_$jscoverage['nodemock.js'][270] = 0;
_$jscoverage['nodemock.js'][271] = 0;
_$jscoverage['nodemock.js'][272] = 0;
_$jscoverage['nodemock.js'][273] = 0;
_$jscoverage['nodemock.js'][274] = 0;
_$jscoverage['nodemock.js'][275] = 0;
_$jscoverage['nodemock.js'][276] = 0;
_$jscoverage['nodemock.js'][277] = 0;
_$jscoverage['nodemock.js'][278] = 0;
_$jscoverage['nodemock.js'][279] = 0;
_$jscoverage['nodemock.js'][280] = 0;
_$jscoverage['nodemock.js'][282] = 0;
_$jscoverage['nodemock.js'][284] = 0;
_$jscoverage['nodemock.js'][281] = 0;
_$jscoverage['nodemock.js'][285] = 0;
_$jscoverage['nodemock.js'][286] = 0;

@@ -134,17 +133,24 @@ _$jscoverage['nodemock.js'][288] = 0;

_$jscoverage['nodemock.js'][292] = 0;
_$jscoverage['nodemock.js'][293] = 0;
_$jscoverage['nodemock.js'][295] = 0;
_$jscoverage['nodemock.js'][294] = 0;
_$jscoverage['nodemock.js'][296] = 0;
_$jscoverage['nodemock.js'][297] = 0;
_$jscoverage['nodemock.js'][298] = 0;
_$jscoverage['nodemock.js'][299] = 0;
_$jscoverage['nodemock.js'][301] = 0;
_$jscoverage['nodemock.js'][302] = 0;
_$jscoverage['nodemock.js'][303] = 0;
_$jscoverage['nodemock.js'][304] = 0;
_$jscoverage['nodemock.js'][305] = 0;
_$jscoverage['nodemock.js'][306] = 0;
_$jscoverage['nodemock.js'][309] = 0;
_$jscoverage['nodemock.js'][314] = 0;
_$jscoverage['nodemock.js'][316] = 0;
_$jscoverage['nodemock.js'][317] = 0;
_$jscoverage['nodemock.js'][307] = 0;
_$jscoverage['nodemock.js'][311] = 0;
_$jscoverage['nodemock.js'][312] = 0;
_$jscoverage['nodemock.js'][315] = 0;
_$jscoverage['nodemock.js'][320] = 0;
_$jscoverage['nodemock.js'][321] = 0;
_$jscoverage['nodemock.js'][322] = 0;
_$jscoverage['nodemock.js'][323] = 0;
_$jscoverage['nodemock.js'][326] = 0;
_$jscoverage['nodemock.js'][327] = 0;
_$jscoverage['nodemock.js'][330] = 0;
_$jscoverage['nodemock.js'][331] = 0;
_$jscoverage['nodemock.js'][332] = 0;
_$jscoverage['nodemock.js'][333] = 0;
}

@@ -411,44 +417,50 @@ _$jscoverage['nodemock.js'][27]++;

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

@@ -458,43 +470,43 @@ }

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

@@ -504,5 +516,5 @@ }

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

@@ -513,23 +525,32 @@ }

}
_$jscoverage['nodemock.js'][305]++;
_$jscoverage['nodemock.js'][311]++;
return true;
}
_$jscoverage['nodemock.js'][306]++;
_$jscoverage['nodemock.js'][312]++;
;
_$jscoverage['nodemock.js'][309]++;
_$jscoverage['nodemock.js'][315]++;
this.mock(methodName);
}
_$jscoverage['nodemock.js'][314]++;
_$jscoverage['nodemock.js'][320]++;
if (typeof exports != "undefined") {
_$jscoverage['nodemock.js'][316]++;
_$jscoverage['nodemock.js'][322]++;
exports.mock = (function (methodName) {
_$jscoverage['nodemock.js'][317]++;
_$jscoverage['nodemock.js'][323]++;
return new NodeMock(methodName);
});
_$jscoverage['nodemock.js'][320]++;
_$jscoverage['nodemock.js'][326]++;
exports.fail = (function () {
_$jscoverage['nodemock.js'][321]++;
_$jscoverage['nodemock.js'][327]++;
return new NodeMock();
});
_$jscoverage['nodemock.js'][330]++;
exports.ignore = (function (method) {
_$jscoverage['nodemock.js'][331]++;
var nm = new NodeMock();
_$jscoverage['nodemock.js'][332]++;
nm.ignore(method);
_$jscoverage['nodemock.js'][333]++;
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//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};","}"];
_$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};","}"];

@@ -248,2 +248,8 @@ /**

//ignore the mock
this.ignore = function(method) {
this[method] = function() {};
};
//method for cloning entry

@@ -324,2 +330,8 @@ function cloneEntry(entry) {

};
exports.ignore = function(method) {
var nm = new NodeMock();
nm.ignore(method);
return nm;
};
}
{
"name": "nodemock",
"version": "0.2.12",
"version": "0.2.13",
"directories": {

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

@@ -26,5 +26,4 @@ Node Mock - Simple Yet Powerful Mocking Framework for NodeJs

-------
Node JS can be used with any testing framework. And we've used it with
Nodeunit and it's a perfect match.
(https://github.com/arunoda/nodemock/blob/master/test/nodemock.js "Nodemock with Nodeunit")
Node JS can be used with any testing framework. And we've used it with Nodeunit and it's a perfect match.
[See Examples](https://github.com/arunoda/nodemock/blob/master/test/nodemock.js "Nodemock with Nodeunit")

@@ -31,0 +30,0 @@ Install

@@ -415,2 +415,31 @@ /*

test.done();
};
exports.testIgnore = function(test) {
var mock = nm.mock('foo').returns(100);
test.equal(mock.foo(), 100);
mock.ignore('hello');
test.ok(mock.hello);
test.doesNotThrow(function() {
mock.hello();
});
test.done();
};
exports.testIgnoreRootMethod = function(test) {
var mock = nm.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