Skip to content

Instantly share code, notes, and snippets.

@leobalter
Forked from JamesMGreene/interface-for-reporters.js
Last active August 29, 2015 14:01
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 leobalter/226637d148577ac9b5c3 to your computer and use it in GitHub Desktop.
Save leobalter/226637d148577ac9b5c3 to your computer and use it in GitHub Desktop.
var reporterInterface = (function() {
function start( data ) {
/** PROCESS DATA **/
output( "TAP version 13" );
}
function suiteStart( data ) {
/** PROCESS DATA **/
var resp = comment( data.name + " suite" );
output( resp );
}
function testStart( data ) {
/** PROCESS DATA **/
var resp = comment( data.name + " test" );
output( resp );
output( "1.." + data.plan );
}
function testEnd( data ) {
/** PROCESS DATA **/
// most likely to be related to the assertion, not?
var testLine = ( data.pass ? "ok" : "not ok" );
testLine += " " + comment( data.name );
output
}
function suiteEnd( data ) {
/** PROCESS DATA **/
}
function end( data ) {
/** PROCESS DATA **/
// # tests 2
// # pass 1
// # fail 1
var tests = comment( "tests " + data.tests );
var pass = comment( "pass " + data.pass );
var fail = comment( "fail " + data.fail );
output( [ tests, pass, fail ].join( '\n' ) );
}
function assert( data ) {
/** PROCESS DATA **/
}
// abstracts output method, like console.log
function output( text ) {
console.log( text );
}
function comment( text ) {
return "# " + text;
}
return {
start: start,
suiteStart: suiteStart,
testStart: testStart,
testEnd: testEnd,
suiteEnd: suiteEnd,
end: end,
assert: assert
};
})();
// Jasmine usage
jasmine.getEnv().addReporter( createJasmineReporter( reporterInterface ) );
// Utility method
function createJasmineReporter( reporterInterface ) {
if ( !reporterInterface ) {
return;
}
var reporter = {};
if ( reporterInterface.start ) {
reporter.jasmineStarted = reporterInterface.start;
}
if ( reporterInterface.suiteStart ) {
reporter.suiteStarted = reporterInterface.suiteStart;
}
if ( reporterInterface.testStart ) {
reporter.specStarted = reporterInterface.testStart;
}
if ( reporterInterface.testEnd ) {
reporter.specDone = reporterInterface.testEnd;
}
if ( reporterInterface.suiteEnd ) {
reporter.suiteDone = reporterInterface.suiteEnd;
}
if ( reporterInterface.end ) {
reporter.jasmineDone = reporterInterface.end;
}
return reporter;
}
// Mocha usage
mocha.reporter( createMochaReporter( reporterInterface ) );
// Utility method
function createMochaReporter( reporterInterface ) {
if ( !reporterInterface ) {
return;
}
var reporterCtor = function( runner ) {
if ( reporterInterface.start ) {
runner.on( "start", reporterInterface.start );
}
if ( reporterInterface.suiteStart ) {
runner.on( "suite", reporterInterface.suiteStart );
}
if ( reporterInterface.testStart ) {
runner.on( "test", reporterInterface.testStart );
}
if ( reporterInterface.testEnd ) {
runner.on( "test end", reporterInterface.testEnd );
}
if ( reporterInterface.suiteEnd ) {
runner.on( "suite end", reporterInterface.suiteEnd );
}
if ( reporterInterface.end ) {
runner.on( "end", reporterInterface.end );
}
};
return reporterCtor;
}
// QUnit usage
addQUnitReporter( QUnit, reporterInterface );
// Utility method
function addQUnitReporter( QUnit, reporterInterface ) {
if ( !( QUnit && reporterInterface ) ) {
return;
}
if ( reporterInterface.start ) {
QUnit.begin( reporterInterface.start );
}
if ( reporterInterface.suiteStart ) {
QUnit.moduleStart( reporterInterface.suiteStart );
}
if ( reporterInterface.testStart ) {
QUnit.testStart( reporterInterface.testStart );
}
if ( reporterInterface.testEnd ) {
QUnit.testDone( reporterInterface.testEnd );
}
if ( reporterInterface.suiteEnd ) {
QUnit.moduleDone( reporterInterface.suiteEnd );
}
if ( reporterInterface.end ) {
QUnit.done( reporterInterface.end );
}
if ( reporterInterface.assert ) {
QUnit.log( assertionFn );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment