Created
December 22, 2009 23:21
-
-
Save fabiomcosta/262167 to your computer and use it in GitHub Desktop.
A Console Jasmine Runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a console object that outputs at the terminal | |
load('shell_console.js'); | |
// makes jasmine-0.10.0 stop breaking | |
var window = this; | |
window.setTimeout = function(){}; | |
window.setInterval = function(){}; | |
window.clearTimeout = function(){}; | |
window.clearInterval = function(){}; | |
XMLHttpRequest = function(){}; | |
load('../../jasmine/lib/jasmine-0.10.0.js'); | |
jasmine.DEFAULT_UPDATE_INTERVAL = 0; // needed so jasmine wont call setTimeout method | |
window = null; // ok we dont have a window, i was joking | |
XMLHttpRequest = null; | |
describe('Example', function(){ | |
var increment = 0; | |
var list = []; | |
it('should pass', function(){ | |
expect(0).toBe(0); | |
}); | |
it('should fail', function(){ | |
expect(0).toBe(1); | |
}); | |
it('beforeEach should have incremented', function(){ | |
expect(increment).toBe(3); | |
}); | |
it('afterEach should have appended an element to the list', function(){ | |
expect(list.length).toBe(3); | |
}); | |
}); | |
load('ShellReporter.js'); | |
var jasmineEnv = jasmine.getEnv(); | |
jasmineEnv.reporter = new jasmine.ShellReporter(); | |
jasmineEnv.execute(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// shell console | |
(function(window) { | |
window.console = window.console || {}; | |
var colors = { | |
HEADER : '\033[95m', | |
BLUE : '\033[1;34m', | |
GREEN : '\033[92m', | |
YELLOW : '\033[93m', | |
RED : '\033[91m', | |
NC : '\033[0m' // no color | |
}; | |
// these functions will have colored outputs | |
var messageColors = { | |
'info': colors.BLUE, | |
'warn': colors.YELLOW, | |
'error': colors.RED, | |
'ok': colors.GREEN | |
}; | |
var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', | |
'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd', 'ok']; | |
logFunction = window.print ? function(args, name){ | |
var prefix = messageColors[name] || ''; | |
var sufix = prefix ? colors.NC : ''; | |
window.print(prefix + Array.prototype.toString.call(args) + sufix); | |
} : function(){}; | |
var fnName = null; | |
for(var i = 0; i < names.length; i++){ | |
fnName = names[i]; | |
window.console[fnName] = window.console[fnName] || (function(fnName){ | |
return function(){ | |
logFunction(arguments, fnName); | |
}; | |
})(fnName); | |
} | |
window.console.colors = colors; | |
})(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jasmine.ShellReporter = function(){ | |
}; | |
jasmine.ShellReporter.prototype.reportRunnerStarting = function(runner){ | |
this.startedAt = new Date(); | |
}; | |
jasmine.ShellReporter.prototype.reportRunnerResults = function(runner){ | |
var results = runner.results(); | |
var specs = runner.specs(); | |
var specCount = 0; | |
for(var i = 0; i < specs.length; i++){ | |
if(this.specFilter(specs[i])){ | |
specCount++; | |
} | |
} | |
var message = '' + specCount + ' spec' + | |
(specCount === 1 ? '' : 's' ) + ', ' + | |
(results.failedCount === 0 ? console.colors.GREEN : console.colors.RED) + results.failedCount + ' failure' + | |
(results.failedCount === 1 ? '' : 's') + console.colors.BLUE + ' in ' + | |
((new Date().getTime() - this.startedAt.getTime()) / 1000) + 's'; | |
console.info(message); | |
}; | |
jasmine.ShellReporter.prototype.reportSuiteResults = function(suite){ | |
}; | |
jasmine.ShellReporter.prototype.reportSpecResults = function(spec){ | |
var results = spec.results(); | |
var status = results.passed() ? 'passed' : 'failed'; | |
if(results.skipped){ | |
status = 'skipped'; | |
} | |
var resultItems = results.getItems(); | |
for(var i = 0; i < resultItems.length; i++){ | |
var result = resultItems[i]; | |
if(result.passed && !result.passed()){ | |
console.error(result.trace.name + ' on file ' + result.trace.fileName + ' line ' + result.trace.lineNumber + '. ' + result.message); | |
}else{ | |
console.ok('PASS'); | |
} | |
} | |
}; | |
jasmine.ShellReporter.prototype.log = function(){ | |
console.log.apply(console, arguments); | |
}; | |
jasmine.ShellReporter.prototype.getLocation = function(){ | |
return ''; | |
}; | |
jasmine.ShellReporter.prototype.specFilter = function(spec){ | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment