🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

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

to
0.8.0

lib/invoker.js
'use strict';
const Action = require('./action');
const Invoker = require('./invoker');
const Cardinality = require('./cardinality');

@@ -187,2 +188,38 @@ const matcher = require('./matcher');

/** Adds a new invoker action, that will call callback passed
* on execution time with given arguments. Newly created Invoker
* has expected execution count set to 1.
*
* @returns {Expectation}
* Returns current instance of the expectation for chaining.
*/
willOnceInvoke(/* ... */) {
tryUpdateCardinality.call(this, 1);
addAction.call(this, new Invoker(Array.from(arguments), 1));
return this;
}
/** Adds a new invoker action, that will call callback passed
* on execution time with given arguments. Newly created Invoker
* has expected execution count set to 2.
*
* @returns {Expectation}
* Returns current instance of the expectation for chaining.
*/
willTwiceInvoke(action) {
tryUpdateCardinality.call(this, 2);
addAction.call(this, new Invoker(Array.from(arguments), 2));
return this;
}
/** Adds a new invoker action, that will call callback passed
* on execution time with given arguments. Newly created Invoker
* has unbounded execution time.
*/
willRepeatedlyInvoke(action) {
tryUpdateCardinality.call(this, -1);
addAction.call(this, new Invoker(Array.from(arguments), -1));
this.actionsFinalized = true;
}
/**

@@ -189,0 +226,0 @@ * @param {Array} args

2

package.json
{
"name": "jsmock",
"version": "0.7.0",
"version": "0.8.0",
"description": "Mocking framework for javascript",

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

@@ -32,3 +32,4 @@ [![npm Package](https://img.shields.io/npm/v/jsmock.svg?style=flat-square)](https://www.npmjs.org/package/jsmock)

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 expectation to be thrown.
have been replaced and any call to *foo.bar* will cause an UnexpectedCall error
to be thrown.

@@ -48,3 +49,2 @@ ```javascript

## Specifying Matcher

@@ -118,2 +118,17 @@ Matcher validates that call of mocked function is valid for given expectation. If

In js and nodejs it's very common to provide callback as the last argument in the
function call. Often the only purpose of the mock is to execute that callback with some
predefined arguments. This kind of action can be created easily using *Invoke* versions
of already presented functions:
```javascript
fsMock.expectCall('readdir')
.willOnceInvoke(null, ['a.js', 'b.js']);
// Which is equivalent of
fsMock.expectCall('readdir')
.willOnceInvoke((path, cb) => cb(null, ['a.js', 'b.js']));
```
### Actions and Cardinality

@@ -120,0 +135,0 @@ Combination of cardinality and action specifiers can build virtually any expectation.

@@ -74,3 +74,3 @@ 'use strict';

fsMock.expectCall('readdir')
.willOnce((p, cb) => cb(new Error('TESTERROR')));
.willOnceInvoke(new Error('TESTERROR'));

@@ -86,10 +86,10 @@ subdirectoriesFs('/some/wrong/path', (err, dirs) => {

.matching((p, cb) => p === '.')
.willOnce((p, cb) => cb(null, ['a', 'b', 'c', 'd', 'e', 'f']));
.willOnceInvoke(null, ['a', 'b', 'c', 'd', 'e', 'f']);
fsMock.expectCall('stat')
.times(6)
.willOnce((p, cb) => cb(null, {isDirectory: () => true}))
.willOnce((p, cb) => cb(null, {isDirectory: () => false}))
.willOnce((p, cb) => cb(null, {isDirectory: () => true}))
.willRepeatedly((p, cb) => cb(null, {isDirectory: () => false}))
.willOnceInvoke(null, {isDirectory: () => true})
.willOnceInvoke(null, {isDirectory: () => false})
.willOnceInvoke(null, {isDirectory: () => true})
.willRepeatedlyInvoke(null, {isDirectory: () => false})

@@ -105,10 +105,10 @@ subdirectoriesFs('.', (err, dirs) => {

fsMock.expectCall('readdir')
.willOnce((p, cb) => cb(null, ['a', 'b', 'c', 'd', 'e', 'f']));
.willOnceInvoke(null, ['a', 'b', 'c', 'd', 'e', 'f']);
fsMock.expectCall('stat')
.times(6)
.willOnce((p, cb) => cb(null, {isDirectory: () => true}))
.willOnce((p, cb) => cb(new Error('TESTERROR')))
.willOnce((p, cb) => cb(null, {isDirectory: () => false}))
.willRepeatedly((p, cb) => cb(null, {isDirectory: () => true}))
.willOnceInvoke(null, {isDirectory: () => true})
.willOnceInvoke(new Error('TESTERROR'))
.willOnceInvoke(null, {isDirectory: () => false})
.willRepeatedlyInvoke(null, {isDirectory: () => true})

@@ -115,0 +115,0 @@ subdirectoriesFs('/some/wrong/path', (err, dirs) => {

@@ -257,4 +257,106 @@ 'use strict';

expect(exp.willRepeatedly.bind(exp, 1)).to.throw(Error);
expect(exp.willOnceInvoke.bind(exp, 1)).to.throw(Error);
expect(exp.willTwiceInvoke.bind(exp, 1)).to.throw(Error);
expect(exp.willRepeatedlyInvoke.bind(exp, 1)).to.throw(Error);
});
});
describe('willOnceInvoke', () => {
it('Should create invoker with counter set to 1', () => {
let exp = new Expectation();
exp.willOnceInvoke(null, 1);
expect(exp.actions).to.have.length(1);
expect(exp.actions[0].counter).to.be.equal(1);
});
it('Should return instance of current expectation', () => {
let exp1 = new Expectation();
let exp2 = new Expectation();
expect(exp1.willOnceInvoke(true)).to.be.equal(exp1);
expect(exp2.willOnceInvoke(false)).to.be.equal(exp2);
})
it('Should create invoker with provided arguments', (done) => {
let exp = new Expectation();
exp.willOnceInvoke(1, 2);
exp.willOnceInvoke(true, 'Hello World');
exp.actions[0].execute([(a, b) => {
expect(a).to.be.equal(1);
expect(b).to.be.equal(2);
}]);
exp.actions[1].execute([(a, b) => {
expect(a).to.be.true;
expect(b).to.be.equal('Hello World');
done();
}]);
});
});
describe('willTwiceInvoke', () => {
it('Should create invoker with counter set to 2', () => {
let exp = new Expectation();
exp.willTwiceInvoke(null, 1);
expect(exp.actions).to.have.length(1);
expect(exp.actions[0].counter).to.be.equal(2);
});
it('Should return instance of current expectation', () => {
let exp1 = new Expectation();
let exp2 = new Expectation();
expect(exp1.willTwiceInvoke(true)).to.be.equal(exp1);
expect(exp2.willTwiceInvoke(false)).to.be.equal(exp2);
})
it('Should create invoker with provided arguments', (done) => {
let exp = new Expectation();
exp.willTwiceInvoke(1, 2);
exp.willTwiceInvoke(true, 'Hello World');
exp.actions[0].execute([(a, b) => {
expect(a).to.be.equal(1);
expect(b).to.be.equal(2);
}]);
exp.actions[1].execute([(a, b) => {
expect(a).to.be.true;
expect(b).to.be.equal('Hello World');
done();
}]);
});
});
describe('willRepeatedlyInvoke', () => {
it('Should create invoker with counter set to negative value', () => {
let exp = new Expectation();
exp.willRepeatedlyInvoke(1,3);
expect(exp.actions).to.have.length(1);
expect(exp.actions[0].counter).to.be.lessThan(0);
});
it('Should create invoker with provided arguments', (done) => {
let exp = new Expectation();
exp.willRepeatedlyInvoke(1, 2);
exp.actions[0].execute([(a, b) => {
expect(a).to.be.equal(1);
expect(b).to.be.equal(2);
}]);
exp = new Expectation();
exp.willRepeatedlyInvoke(true, 'Hello World');
exp.actions[0].execute([(a, b) => {
expect(a).to.be.true;
expect(b).to.be.equal('Hello World');
done();
}]);
});
it('Should block possibility of adding any more actions to the expectation', () => {
let exp = new Expectation();
exp.willRepeatedlyInvoke(4);
expect(exp.willOnce.bind(exp, 1)).to.throw(Error);
expect(exp.willTwice.bind(exp, 1)).to.throw(Error);
expect(exp.willRepeatedly.bind(exp, 1)).to.throw(Error);
expect(exp.willOnceInvoke.bind(exp, 1)).to.throw(Error);
expect(exp.willTwiceInvoke.bind(exp, 1)).to.throw(Error);
expect(exp.willRepeatedlyInvoke.bind(exp, 1)).to.throw(Error);
});
});
});

@@ -261,0 +363,0 @@