Socket
Socket
Sign inDemoInstall

jsmock

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsmock - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

lib/matcher.js

9

index.js

@@ -1,1 +0,8 @@

module.exports = require('./lib/mock');
const matcher = require('./lib/matcher');
const Mock = require('./lib/mock');
module.exports = {
_: matcher.any,
Mock: Mock
}

11

lib/action.js

@@ -16,7 +16,5 @@ 'use strict';

* @param {Expectation} owner
* @param {Function} action
* @param {Any} action
* @param {Number} counter
*
* @throws {TypeError}
* TypeError is thrown if specified action is not a valid Function object.
* @throws {TypeErrpr}

@@ -30,5 +28,6 @@ * TypeError is thrown if specified number is defined and its type is not a Number.

this.owner = owner;
this.action = action || NO_ACTION;
if(typeof this.action !== 'function') {
throw new TypeError('Provided action should be a valid function');
if(typeof action !== 'function') {
this.action = () => action;
} else {
this.action = action || NO_ACTION;
}

@@ -35,0 +34,0 @@ this.setCounter((typeof counter === 'undefined') ? 1 : counter);

'use strict';
const Action = require('./action');
const matcher = require('./matcher');
const _ = require('lodash');
/** @function matcher
* Creates apropriate matcher function for given arguments.
*/
function matcher(/* ... */) {
if(arguments.length === 0) {
return () => true;
} else if((arguments.length === 1) && (typeof arguments[0] === 'function' )) {
return arguments[0];
} else {
const expected = Array.from(arguments);
return function () {
return _.isEqual(Array.from(arguments), expected);
}
}
}
/** @class Expectation

@@ -38,3 +23,3 @@ *

constructor(args) {
this.matcher = matcher.apply(null, args);
this.matcher = matcher.create.apply(null, args);
this.actions = [];

@@ -101,3 +86,3 @@ }

matching(/* ... */) {
this.matcher = matcher.apply(null, arguments);
this.matcher = matcher.create.apply(null, arguments);
return this;

@@ -104,0 +89,0 @@ }

{
"name": "jsmock",
"version": "0.1.0",
"version": "0.2.0",
"description": "Mocking framework for javascript",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -0,4 +1,82 @@

[![npm Package](https://img.shields.io/npm/v/jsmock.svg?style=flat-square)](https://www.npmjs.org/package/jsmock)
[![Build Status](https://travis-ci.org/kcwiakala/jsmock.svg?branch=master)](https://travis-ci.org/kcwiakala/jsmock)
# jsmock
Mocking framework for javascript
Mocking framework for javascript, inspired by googlemock C++ framework.
This project is still under construction ...
# Installation
*jsmock* is published on npm
> npm install --save-dev jsmock
# User Guide
## Creating Mocks
```javascript
const Mock = require('jsmock').Mock;
let foo = {
bar: (a, b) => {
return a + b
}
};
let fooMock = new Mock(foo);
```
Now *fooMock* is a mock object wrapping *foo*. All functions of original object
have been replaced and any call to *foo.bar* will cause an expection to be thrown.
```javascript
expect(foo.bar.bind(foo)).to.throw(Error);
```
## Defining Expectation
Expectation of function call is defined by calling *expectCall* on mock object.
```javascript
fooMock.expectCall('bar');
```
By default this will setup an expectation of single call to foo.bar function with
any arguments. As there is no action specified yet, nothing will be returned and
no side effects will be observed.
## Specifying Matcher
Matcher validates that call of mocked function is valid for given expectation. If
no explicit matcher is specified expectation will be executed for any call to
mocked function. Matcher can be specified as a predicate or simply as an arguments
list to be verified against actual call.
```javascript
fooMock.expectCall('bar', (a,b) => a > b);
foo.bar(3,2); // OK - 3 > 2
foo.bar(1,4); // KO - excpetion thrown
fooMock.expectCall('bar', 1, 8);
foo.bar(1, 8); // OK
foo.bar(1, 0); // KO
```
Mathcher can be specified directly in arguments of the *expectCall* method or by
calling *matching* function on mock object. Note that *expectCall* returns mock
object making call chain possible:
```javascript
fooMock.expectCall('bar').matching((a,b) => a < b);
fooMock.expectCall('bar').matching(1,4);
```
## Specifying Actions
Action is an object encapsulating function to be executed instead of original
code on mocked object. Besides a function each action specifies also number of
times it's expected to be called. Each expectation can have multiple actions
defined, which will be executed in order of creation.
```javascript
fooMock.expectCall('bar')
.willOnce((a,b) => a * b) // First call will return multiplication of arguments
.will(6).times(3) // Next 3 calls will return value 6
.willRepeteadly((a,b) => b) // All following calls will return second argument
```
You need to pay attention to order of specifying actions. If an action with
unlimited number of expected calls preceeds other actions, it will prevent their
execution and cause mock to be invalid.
# Examples

@@ -13,14 +13,2 @@ 'use strict';

describe('constructor', () =>{
it('Should validate that action is a function', () => {
// Passing a non function object should result in exception
expect(create.bind(null, Action, this, 1)).to.throw(TypeError, 'function');
expect(create.bind(null, Action, this, 'bla bla')).to.throw(TypeError, 'function');
expect(create.bind(null, Action, this, {})).to.throw(TypeError, 'function');
expect(create.bind(null, Action, this, [])).to.throw(TypeError, 'function');
// No parameter or a valid function should be fine
expect(create.bind(null, Action, this)).not.to.throw;
expect(create.bind(null, Action, this, () => {})).not.to.throw;
});
it('Should validate that provided counter is non zero number', () => {

@@ -96,2 +84,11 @@ const action = () => {};

});
it('Should return given value for non-function actions', () => {
let action1 = new Action(null, 5, 1);
expect(action1.execute()).to.be.equal(5);
let foo = {a: 1, b: false};
let action2 = new Action(null, foo, 1);
expect(action2.execute()).to.be.equal(foo);
});
});

@@ -98,0 +95,0 @@

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