Skip to content

Instantly share code, notes, and snippets.

@soarez
Last active October 29, 2015 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soarez/5136664a86f2ba49ff25 to your computer and use it in GitHub Desktop.
Save soarez/5136664a86f2ba49ff25 to your computer and use it in GitHub Desktop.
var util = require('util');
var EventEmitter = require('events').EventEmitter;
function Spy(f, ctx) {
var ee = new EventEmitter();
spy.called = false;
Object.keys(EventEmitter.prototype).forEach(function(p) {
spy[p] = transfer;
function transfer() {
var args = Array.prototype.slice.call(arguments);
return ee[p].apply(ee, args);
}
});
return spy;
function spy() {
var args = Array.prototype.slice.call(arguments);
spy.called = true;
spy.emit('called', args);
var ret = f.apply(ctx || null, args);
spy.emit('returned', args, ret);
return ret;
}
}
// Usage
function theCallback() {
}
var spy = Spy(theCallback);
spy.on('called', function() {
console.log('Gotcha!', spy.called);
});
spy();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment