Socket
Socket
Sign inDemoInstall

nodemock

Package Overview
Dependencies
0
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.16 to 0.2.17

4

lib-cov/nodemock.js

@@ -170,3 +170,3 @@ /* automatically generated by JSCoverage - do not edit */

_$jscoverage['nodemock.js'][40]++;
entry = entries[method][index];
var entry = entries[method][index];
_$jscoverage['nodemock.js'][42]++;

@@ -551,2 +551,2 @@ if (entry.executed == false && deepObjectCheck(entry.args, args)) {

}
_$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.shouldFail && 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};","}"];
_$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\tvar entry = 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.shouldFail && 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};","}"];

@@ -40,3 +40,3 @@ /**

for(var index in entries[method]) {
entry = entries[method][index];
var entry = entries[method][index];

@@ -43,0 +43,0 @@ if(entry.executed == false && deepObjectCheck(entry.args, args)) {

{
"name": "nodemock",
"version": "0.2.16",
"version": "0.2.17",
"directories": {

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

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

mockes.foo(20, function(num, arr) {
mocked.foo(20, function(num, arr) {
console.log(num); //prints 30

@@ -53,0 +53,0 @@ console.log(arr); //prints 40

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc