Skip to content

Instantly share code, notes, and snippets.

@soarez soarez/spy.js
Last active Oct 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.