├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json ├── src ├── createSpy.js └── spyOn.js └── test ├── createSpy.js ├── index.js └── spyOn.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 'stable' 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Sterling Whitley 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://i.imgur.com/XMSlWqI.png?1) 2 | # Espionage 3 | ![](https://travis-ci.org/sterlingw/espionage.svg?branch=master) 4 | [![Dependency Status](https://david-dm.org/sterlingw/espionage.svg)](https://david-dm.org/sterlingw/espionage) 5 | 6 | Standalone library for creating spies in Node.js. Easy to use. No dependancies. 7 | - Minimal 8 | - Simple API 9 | - No dependencies 10 | - No global variables 11 | - No prototype modification 12 | 13 | ``` 14 | npm install espionage 15 | ``` 16 | 17 | # Usage 18 | Espionage exports two functions for creating spies. 19 | 20 | ## `espionage.spyOn` 21 | Accepts a function. Returns a spy. When called, the returned spy returns the same value as the given function. 22 | ``` 23 | var espionage = require('espionage'); 24 | 25 | function add5(num) { 26 | return num + 5; 27 | } 28 | 29 | var spy = espionage.spyOn(add5); // returns a spy 30 | 31 | spy(2); // returns 7 32 | ``` 33 | 34 | ## `espionage.createSpy` 35 | Doesn't accept arguments. Returns a spy. The spy returns undefined. 36 | ``` 37 | var espionage = require('espionage'); 38 | 39 | var spy = espionage.createSpy(); // returns a spy 40 | 41 | spy(); // returns undefined 42 | ``` 43 | 44 | ## Spies 45 | The functions `espionage.spyOn()` and `espionage.createSpy` both return a spy. Spies have these methods: 46 | 47 | ### `spy.callCount` 48 | Doesn't accept arguments. Returns the number of times the spy has been called. 49 | ``` 50 | var spy = espionage.createSpy(); 51 | 52 | spy(); 53 | spy(); 54 | 55 | spy.callCount(); // returns 2 56 | ``` 57 | 58 | ### `spy.wasCalled` 59 | Doesn't accept arguments. Returns a boolean indicating if the spy has been called. 60 | ``` 61 | var spy = espionage.createSpy(); 62 | 63 | spy(); 64 | 65 | spy.wasCalled(); // returns true 66 | ``` 67 | 68 | ### `spy.wasCalledWith` 69 | Accepts a single argument. Returns a boolean indicating if the spy has been called with the given argument. 70 | ``` 71 | function add2(num) { 72 | return num + 2; 73 | } 74 | 75 | var spy = espionage.spyOn(add2); 76 | 77 | spy(2); 78 | 79 | spy.wasCalledWith(2); // returns true 80 | ``` 81 | 82 | ### `spy.returned` 83 | Accepts a single argument. Returns a boolean indicating if the spy has been called with the given argument. 84 | ``` 85 | function add2(num) { 86 | return num + 2; 87 | } 88 | 89 | var spy = espionage.spyOn(add2); 90 | 91 | spy(2); 92 | 93 | spy.returned(4); // returns true 94 | ``` 95 | 96 | # Running tests 97 | `npm test` 98 | 99 | # License 100 | MIT. Copyright (c) [Sterling Whitley](http://sterlingw.com) 101 | 102 | Icon made by [Freepik](http://www.freepik.com) from [Flaticon](http://www.flaticon.com) is licensed under [CC BY 3.0](http://creativecommons.org/licenses/by/3.0/) 103 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | spyOn: require('./src/spyOn'), 3 | createSpy: require('./src/createSpy') 4 | }; 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "espionage", 3 | "version": "1.0.0", 4 | "description": "Standalone library for creating spies in Node.js. Easy to use. No dependancies.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "tape 'test/*.js'" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/sterlingw/SimpleSpy.js.git" 12 | }, 13 | "author": { 14 | "name": "Sterling Whitley", 15 | "email": "sterlingsrq@gmail.com", 16 | "url": "sterlingw.com" 17 | }, 18 | "keywords": [ 19 | "spy", 20 | "spies", 21 | "spy", 22 | "on", 23 | "spyon", 24 | "createspy", 25 | "simple", 26 | "spy", 27 | "simplespy", 28 | "simple-spy", 29 | "simplespy.js", 30 | "espionage", 31 | "espionage.js" 32 | ], 33 | "license": "MIT", 34 | "devDependencies": { 35 | "tape": "^4.2.2" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/createSpy.js: -------------------------------------------------------------------------------- 1 | var spyOn = require('./spyOn'); 2 | 3 | /** 4 | * @return {function} spy 5 | */ 6 | module.exports = function createSpy() { 7 | return spyOn(function(){}); 8 | } 9 | -------------------------------------------------------------------------------- /src/spyOn.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @return {function} spy 3 | */ 4 | module.exports = function spyOn(func) { 5 | var calls = 0, 6 | argumentVals = [], 7 | returnVals = []; 8 | 9 | /** 10 | * @return - the same return value of the given function 11 | */ 12 | function spy(){ 13 | var returnVal = func.apply(this, arguments); 14 | 15 | argumentVals = argumentVals.concat(Array.from(arguments)); 16 | returnVals.push(returnVal); 17 | calls++; 18 | 19 | return returnVal; 20 | }; 21 | 22 | /** 23 | * @return {number} 24 | */ 25 | spy.callCount = function(){ 26 | return calls; 27 | }; 28 | 29 | /** 30 | * @return {boolean} 31 | */ 32 | spy.wasCalled = function(){ 33 | return returnVals.length > 0; 34 | }; 35 | 36 | /** 37 | * @return {boolean} 38 | */ 39 | spy.wasCalledWith = function(val) { 40 | return argumentVals.indexOf(val) > -1; 41 | }; 42 | 43 | /** 44 | * @return {boolean} 45 | */ 46 | spy.returned = function(val) { 47 | return returnVals.indexOf(val) > -1; 48 | }; 49 | 50 | return spy; 51 | }; 52 | -------------------------------------------------------------------------------- /test/createSpy.js: -------------------------------------------------------------------------------- 1 | var test = require('tape'); 2 | var espionage = require('../index.js'); 3 | 4 | test('espionage#createSpy returns a spy', function(t){ 5 | var spy = espionage.createSpy(); 6 | 7 | t.equal(typeof spy.callCount, 'function'); 8 | t.equal(typeof spy.wasCalled, 'function'); 9 | 10 | t.end(); 11 | }); 12 | 13 | test('spy returns undefined', function(t){ 14 | var spy = espionage.createSpy(); 15 | 16 | t.equal(typeof spy(), 'undefined'); 17 | 18 | t.end(); 19 | }); 20 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var test = require('tape'); 2 | var espionage = require('../index.js'); 3 | 4 | test('espionage has methods spyOn and createSpy', function(t){ 5 | t.equal(typeof espionage.spyOn, 'function'); 6 | t.equal(typeof espionage.createSpy, 'function'); 7 | t.end(); 8 | }); 9 | -------------------------------------------------------------------------------- /test/spyOn.js: -------------------------------------------------------------------------------- 1 | var test = require('tape'); 2 | var espionage = require('../index.js'); 3 | 4 | function add5(num) { 5 | return num + 5; 6 | } 7 | 8 | test('espionage#spyOn returns a function', function(t){ 9 | var spy = espionage.spyOn(function(){}); 10 | 11 | t.equal(typeof spy, 'function'); 12 | 13 | t.end(); 14 | }); 15 | 16 | test('spy returns the same value as the argument function', function(t){ 17 | var spy = espionage.spyOn(add5); 18 | 19 | t.equal(spy(2), add5(2)); 20 | 21 | t.end(); 22 | }); 23 | 24 | test('spy#callCount returns the number of times a function has been called', function(t){ 25 | var spy = espionage.spyOn(add5) 26 | 27 | spy(); 28 | spy(); 29 | 30 | t.equal(spy.callCount(), 2); 31 | 32 | t.end(); 33 | }); 34 | 35 | test('spy#wasCalled returns true when the spy has been called', function(t){ 36 | var spy = espionage.spyOn(add5); 37 | 38 | spy(); 39 | 40 | t.equal(spy.wasCalled(), true); 41 | 42 | t.end(); 43 | }); 44 | 45 | test('spy#wasCalled returns false when the spy has not been called', function(t){ 46 | var spy = espionage.spyOn(add5); 47 | 48 | t.equal(spy.wasCalled(), false); 49 | 50 | t.end(); 51 | }); 52 | 53 | test('spy#wasCalledWith returns true when the spy was called with the given value', function(t){ 54 | var spy = espionage.spyOn(add5); 55 | var arg = 4; 56 | 57 | spy(arg); 58 | 59 | t.equal(spy.wasCalledWith(arg), true); 60 | 61 | t.end(); 62 | }); 63 | 64 | test('spy#wasCalledWith returns false when the spy was not called with the given value', function(t){ 65 | var spy = espionage.spyOn(add5); 66 | 67 | spy(); 68 | 69 | t.equal(spy.wasCalledWith(4), false); 70 | 71 | t.end(); 72 | }); 73 | 74 | test('spy#returned returns true when the spy has returned the given value', function(t){ 75 | var spy = espionage.spyOn(add5); 76 | 77 | var returnVal = spy(5); 78 | spy(4); 79 | spy(3); 80 | spy(2); 81 | spy(1); 82 | 83 | t.equal(spy.returned(returnVal), true); 84 | 85 | t.end(); 86 | }); 87 | 88 | test('spy#returned returns false when the spy has not returned the given value', function(t){ 89 | var spy = espionage.spyOn(add5); 90 | 91 | spy(2); 92 | spy(1); 93 | 94 | t.equal(spy.returned(3), false); 95 | 96 | t.end(); 97 | }); 98 | --------------------------------------------------------------------------------