Skip to content

Instantly share code, notes, and snippets.

@ckniffen
Created August 18, 2012 22:55
Show Gist options
  • Save ckniffen/3390262 to your computer and use it in GitHub Desktop.
Save ckniffen/3390262 to your computer and use it in GitHub Desktop.
Jasmine Global Scope Pollution Reporter

Jasmine Global Pollution Reporter

  • Requires underscorejs. download
  • After a spec finishes running it logs any global scope pollution that occurred.
  • After test runner has finished it logs a summary of the global scope pollution.
jasmine.GlobalPollutionReporter = function() {
};
jasmine.GlobalPollutionReporter.prototype.reportRunnerStarting = function(runner) {
this.globalState = scopePollution.getGlobalState();
this.lastState = this.globalState;
};
jasmine.GlobalPollutionReporter.prototype.reportRunnerResults = function(runner) {
var globalStateDiff = scopePollution.getGlobalPollution(this.globalState);
if(globalStateDiff.added.length > 0){
console.log('Overall introduced global variable(s): ', globalStateDiff.added.join(', '));
}
if(globalStateDiff.deleted.length > 0){
console.log('Overall deleted global variable(s): ', globalStateDiff.deleted.join(', '));
}
};
jasmine.GlobalPollutionReporter.prototype.reportSuiteResults = function(suite) {
};
jasmine.GlobalPollutionReporter.prototype.reportSpecStarting = function(spec) {
};
jasmine.GlobalPollutionReporter.prototype.reportSpecResults = function(spec) {
var globalStateDiff = scopePollution.getGlobalPollution(this.lastState);
this.lastState = scopePollution.getGlobalState();
if(globalStateDiff.added.length > 0){
console.log('Spec "' + spec.getFullName() + '"' + ' - Introduced global variable(s): ', globalStateDiff.added.join(', '));
}
if(globalStateDiff.deleted.length > 0){
console.log('Spec "' + spec.getFullName() + '"' + ' - Deleted global variable(s): ', globalStateDiff.deleted.join(', '));
}
};
jasmine.GlobalPollutionReporter.prototype.log = function(str) {
};
var scopePollution = (function(){
function getState(root) {
var state = [];
for ( var key in root ) {
if ( !_.has( root, key )) {
continue;
}
state.push( key );
}
return state;
}
function getPollution(root, oldState){
var newState = getState(root);
return {
'added': _.difference(newState, oldState),
'deleted': _.difference(oldState, newState)
};
}
function getGlobalState(){
return getState(window);
}
function getGlobalPollution(state){
return getPollution(window, state);
}
return {
'getState': getState,
'getPollution': getPollution,
'getGlobalState': getGlobalState,
'getGlobalPollution': getGlobalPollution
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment