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

nodemock

Package Overview
Dependencies
Maintainers
0
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.1.0beta to 0.2.0beta

104

lib/nodemock.js

@@ -30,38 +30,42 @@ /**

var args = [];
var returnValue = undefined;
var callbackIndex = null;
var callbackArgs = null;
var currentMockFunction = null;
var args = [];
var returnValue = [];
var callbackIndex = [];
var callbackArgs = [];
var mockFunction = function() {
var mockFunction = function(method) {
//check for argument length
if(args.length != arguments.length) {
throw "Number of Arguments passed(" + arguments.length +
") is not tally with expected(" + args.length +
") in '" + methodName + "()'";
}
//check for argument content
if(!deepObjectCheck(args, arguments)) {
throw "Arguments content passed is not tally with expected";
}
//calling the callback
if(callbackIndex != null) {
var func = arguments[callbackIndex];
if(typeof(func) == "function") {
func.apply(this, callbackArgs);
} else {
throw "Expected callback is not defined as callback";
return function() {
//check for argument length
if(!this[method]) {
throw "Mock function '" + method + "()' is not defined";
} else if((args[method].length != arguments.length)) {
throw "Number of Arguments passed(" + arguments.length +
") is not tally with expected(" + args[method].length +
") in '" + methodName + "()'";
} else if(!deepObjectCheck(args[method], arguments)) {
//check for argument content
throw "Arguments content passed is not tally with expected";
}
}
return returnValue;
//calling the callback
if(callbackIndex[method]) {
var func = arguments[callbackIndex[method]];
if(typeof(func) == "function") {
func.apply(this, callbackArgs[method] || []);
} else {
throw "Expected callback is not defined as callback";
}
}
return returnValue[method];
};
};
this[methodName] = mockFunction;
this.takes = function() {
args = arguments;
args[currentMockFunction] = arguments;
return this;

@@ -71,3 +75,3 @@ };

this.returns = function(value) {
returnValue = value;
returnValue[currentMockFunction] = value;
return this;

@@ -77,10 +81,36 @@ };

this.calls = function() {
callbackIndex = arguments[0];
callbackArgs = [];
for(var lc =1; lc< arguments.length; lc++) {
callbackArgs.push(arguments[lc]);
if(typeof(arguments[0]) == "number") {
callbackIndex[currentMockFunction] = arguments[0];
} else {
throw "First arg of the calls() should be the index of the callback";
}
if(arguments[1] && (arguments[1] instanceof Array)) {
callbackArgs[currentMockFunction] = arguments[1];
}
return this;
};
//Assign the mocking function
this.mock = function(method) {
//let the current mocking method be this
currentMockFunction = method;
if(!this[method]) {
//ground work
args[method] = [];
returnValue[method] = undefined;
callbackIndex[method] = null;
callbackArgs[method] = null;
//assign the mock method
this[method] = mockFunction(method);
}
return this;
};
var deepObjectCheck = function(expected, actual) {

@@ -107,2 +137,6 @@

};
//initialize the mocking
this.mock(methodName);
}

@@ -109,0 +143,0 @@

{
"name": "nodemock",
"version": "0.1.0beta",
"version": "0.2.0beta",
"directories": {

@@ -11,5 +11,6 @@ "lib": "./lib"

},
"description": "Simple Mocking Framework for NodeJs",
"description": "Simple Yet Powerful Mocking Framework for NodeJs",
"author": "Arunoda Susiripala <arunoda.susiripala@gmail.com>",
"homepage": "https://github.com/arunoda/nodemock",
"license": "The MIT License",
"repository" :

@@ -16,0 +17,0 @@ {

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

NodeMock is a very simple to use mocking framework which can be used to
mock functions in JavaScript objects. You can create mock a function in a single line.
mock functions in JavaScript objects.
NodeMock creates mock methods in less code with more expressive manner

@@ -15,12 +16,6 @@ Features

* Callbacks can also be executed with providing arguments
* Multiple mock functions in one object
* Alter a mock function later on
* Method chaining allows creating mocks super easy
Environment
-----------
NodeMock does not depend on any third party library and then can be used with
any JavaScript environment including
* NodeJS
* Browser
* Rhino
Testing

@@ -31,2 +26,6 @@ -------

Install
---------
npm install nodemock
Usage

@@ -36,7 +35,5 @@ ------

### Load the Module
var nodemock = require("nodemock");
### Creating a mock function with taking arguments and return value
var mocked = nodemock.mock("foo").takes(10, [10, 20, 30]).returns(98);

@@ -49,8 +46,7 @@

### Creating a mock with callback support
var mocked = nodemock.mock("foo").takes(20, function(){}).calls(1, 30, [10, 20]);
var mocked = nodemock.mock("foo").takes(20, function(){}).calls(1, [30, 40]);
mockes.foo(20, function(num, arr) {
console.log(num); //prints 30
console.log(arr); //prints [10, 20]
console.log(arr); //prints 40
});

@@ -60,5 +56,19 @@

When you invoke foo() nodemock will calls the callback(sits in argument index 1 - as specified)
with the parameters 30 and [10, 20] respectively.
with the parameters 30 and 40 respectively.
*/
### Alter an already created mock function
var mocked = nodemock.mock("foo").takes(10).returns(30);
mocked.foo(10); //gives 30
mocked.mock("foo").takes(10, 20);
mocked.foo(10, 20); //gives 30
### Add multiple mock functions
var mocked = nodemock.mock("foo").takes(10).returns(30);
mocked.foo(10); //gives 30
mocked.mock("bar").takes(true).returns(40);
mocked.bar(true); // gives 40
API Documentation

@@ -76,5 +86,5 @@ -----------------

mocked.calls(callbackPosition, callbackArg1, callbackArg2, ...)
mocked.calls(callbackPosition, argumentsArray)
Calls a callback at the arguments in index `callbackPosition`
with the arguments specified above
with the arguments specified in the "argumentsArray"

@@ -85,4 +95,7 @@ when using this you've to define a function signature as a callback in the argument list

`mocked.takes(10, 20, function(){})
mocked.mock(methodName)
Used to alter or create a new mock method and add rules to it as usual
Licence
License
-------

@@ -89,0 +102,0 @@ The MIT License

@@ -61,2 +61,10 @@ /**

exports.testWithJustReturn = function(test) {
var mock = nm.mock("foo").returns(false);
test.ok(!mock.foo());
test.done();
};
exports.testArgumentsDeep = function(test) {

@@ -99,3 +107,3 @@

var mock = nm.mock("foo").takes(10, function(){}).calls(1, 10, 20);
var mock = nm.mock("foo").takes(10, function(){}).calls(1, [10, 20]);
test.expect(2);

@@ -112,3 +120,3 @@ mock.foo(10, function(a, b) {

var mock = nm.mock("foo").calls(1, 10, 20);
var mock = nm.mock("foo").calls(1, [10, 20]);

@@ -127,3 +135,3 @@ test.throws(function() {

var mock = nm.mock("foo").takes(10, function(){}).calls(1, 10, 20, 30, 40);
var mock = nm.mock("foo").takes(10, function(){}).calls(1, [10, 20, 30, 40]);
test.expect(2);

@@ -140,3 +148,3 @@ mock.foo(10, function(a, b) {

var mock = nm.mock("foo").takes(10, function(){}).calls(1, 10);
var mock = nm.mock("foo").takes(10, function(){}).calls(1, [10]);
test.expect(2);

@@ -149,2 +157,79 @@ mock.foo(10, function(a, b) {

test.done();
};
exports.testCallbackNoArgs = function(test) {
var mock = nm.mock("foo").takes(10, function(){}).calls(1, []);
test.expect(2);
mock.foo(10, function(a, b) {
test.equals(a, undefined);
test.equals(b, undefined);
});
test.done();
};
exports.testCallbackNullArgs = function(test) {
var mock = nm.mock("foo").takes(10, function(){}).calls(1, null);
test.expect(2);
mock.foo(10, function(a, b) {
test.equals(a, undefined);
test.equals(b, undefined);
});
test.done();
};
exports.testCallbackWithoutArgs = function(test) {
var mock = nm.mock("foo").takes(10, function(){}).calls(1);
test.expect(2);
mock.foo(10, function(a, b) {
test.equals(a, undefined);
test.equals(b, undefined);
});
test.done();
};
exports.testMockAgain = function(test) {
var mock = nm.mock("foo").takes(10, 20).returns(30);
test.doesNotThrow(function() {
test.equal(mock.foo(10, 20), 30);
});
mock.mock("bar").takes(10).returns(40);
test.doesNotThrow(function() {
test.equal(mock.bar(10), 40);
});
test.doesNotThrow(function() {
test.equal(mock.foo(10, 20), 30);
});
test.done();
};
exports.testMockEdit = function(test) {
var mock = nm.mock("foo").takes(10).returns(30);
test.throws(function() {
test.equal(mock.foo(10, 20), 30);
});
test.doesNotThrow(function() {
mock.mock("foo").takes(10, 20);
test.equal(mock.foo(10, 20), 30);
});
test.doesNotThrow(function() {
mock.mock("foo").takes(function(){}).calls(0, [10]);
mock.foo(function(val) {
test.value(10, val);
});
});
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