Skip to content

Commit

Permalink
Add eslint-config-mdrobny
Browse files Browse the repository at this point in the history
  • Loading branch information
mdrobny committed Jan 31, 2017
1 parent 323c111 commit c7b4271
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: 'eslint-config-mdrobny'
}
8 changes: 5 additions & 3 deletions lib/spy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prefer-rest-params */
/**
* Calls original method but intercepts how many times it was called and with what arguments
* {Object} [object]
Expand All @@ -8,7 +9,7 @@ function spy(object, method) {
throw new Error('Object passed but method is missing');
}
if (object && method && !object[method]) {
throw new Error(`Object does not have method "${method}"`)
throw new Error(`Object does not have method "${method}"`);
}

const isObjectVariant = object && method && object[method];
Expand All @@ -18,15 +19,16 @@ function spy(object, method) {
cachedMethod = object[method].bind(object);
}

const spyFn = function() {
function spyFn() {
const args = Array.prototype.slice.call(arguments);
spyFn.called++;
spyFn.args.push(args);

if (isObjectVariant) {
return cachedMethod.apply(object, args);
}
};
return undefined;
}

spyFn.reset = () => {
spyFn.called = 0;
Expand Down
9 changes: 5 additions & 4 deletions lib/stub.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prefer-rest-params, no-param-reassign */
/**
* Prevents calling original method and instead calls mock function if defined
* intercepts how many times it was called and with what arguments
Expand All @@ -11,7 +12,7 @@ function stub(object, method, fn) {
}

if (object && method && !object[method]) {
throw new Error(`Object does not have method "${method}"`)
throw new Error(`Object does not have method "${method}"`);
}

const isObjectVariant = object && method && object[method];
Expand All @@ -21,17 +22,17 @@ function stub(object, method, fn) {
cachedMethod = object[method].bind(object);
}

const stubFn = function() {
function stubFn() {
const args = Array.prototype.slice.call(arguments);

stubFn.called++;
stubFn.args.push(args);

if (fn) {
return fn.apply(undefined, args);
return fn(...args);
}
return undefined;
};
}

stubFn.reset = () => {
stubFn.called = 0;
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Spy and stub utils for testing",
"main": "index.js",
"scripts": {
"test": "mocha test/*.test.js"
"lint": "eslint . --ignore-path .gitignore",
"test": "mocha test/*.test.js && npm run lint"
},
"engines": {
"node": ">=4"
Expand All @@ -19,13 +20,15 @@
"test",
"testing"
],
"author": "mdrobny",
"author": "Michal Drobniak <[email protected]>",
"license": "ISC",
"bugs": {
"url": "https://github.com/mdrobny/spy-stub/issues"
},
"homepage": "https://github.com/mdrobny/spy-stub#readme",
"devDependencies": {
"eslint": "^3.14.1",
"eslint-config-mdrobny": "^1.0.0",
"mocha": "^3.2.0"
}
}
1 change: 1 addition & 0 deletions test/spy.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require('assert');

const mocha = require('mocha');

const describe = mocha.describe;
const it = mocha.it;

Expand Down
3 changes: 2 additions & 1 deletion test/stub.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require('assert');

const mocha = require('mocha');

const describe = mocha.describe;
const it = mocha.it;

Expand Down Expand Up @@ -121,7 +122,7 @@ describe('Stub', () => {
};
const method = 'method';

const mockFn = () => `mock`;
const mockFn = () => 'mock';
const methodStub = stub(object, method, mockFn);

const result = object.method();
Expand Down

0 comments on commit c7b4271

Please sign in to comment.