Last active
August 29, 2015 13:57
-
-
Save JamesMGreene/9786246 to your computer and use it in GitHub Desktop.
QUnit's WIP proposal for a standardized reporter interface and data structure among JS testing frameworks. The goal is to be able to reuse existing/new reporters in any of the major JS test frameworks.
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
var reporterInterface = { | |
"start": function(data) { | |
/* | |
data: { | |
} | |
*/ | |
}, | |
"suite-start": function(data) { | |
/* | |
data: { | |
} | |
*/ | |
}, | |
"test-start": function(data) { | |
/* | |
data: { | |
} | |
*/ | |
}, | |
"assert": function(data) { | |
/* | |
data: { | |
} | |
*/ | |
}, | |
"test-end": function(data) { | |
/* | |
data: { | |
} | |
*/ | |
}, | |
"suite-end": function(data) { | |
/* | |
data: { | |
} | |
*/ | |
}, | |
"end": function(data) { | |
/* | |
data: { | |
} | |
*/ | |
} | |
}; |
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 usage | |
jasmine.getEnv().addReporter(createJasmineReporter(reporterInterface)); | |
// Utility method | |
function createJasmineReporter(reporterInterface) { | |
if (!reporterInterface) { | |
return; | |
} | |
var runStartFn = reporterInterface["start"]; | |
var suiteStartFn = reporterInterface["suite-start"]; | |
var testStartFn = reporterInterface["test-start"]; | |
var testEndFn = reporterInterface["test-end"]; | |
var suiteEndFn = reporterInterface["suite-end"]; | |
var runEndFn = reporterInterface["end"]; | |
var reporter = {}; | |
if (typeof runStartFn === "function") { | |
reporter.jasmineStarted = runStartFn; | |
} | |
if (typeof suiteStartFn === "function") { | |
reporter.suiteStarted = suiteStartFn; | |
} | |
if (typeof testStartFn === "function") { | |
reporter.specStarted = testStartFn; | |
} | |
if (typeof testEndFn === "function") { | |
reporter.specDone = testEndFn; | |
} | |
if (typeof suiteEndFn === "function") { | |
reporter.suiteDone = suiteEndFn; | |
} | |
if (typeof runEndFn === "function") { | |
reporter.jasmineDone = runEndFn; | |
} | |
return reporter; | |
} |
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
// Mocha usage | |
mocha.reporter(createMochaReporter(reporterInterface)); | |
// Utility method | |
function createMochaReporter(reporterInterface) { | |
if (!reporterInterface) { | |
return; | |
} | |
var runStartFn = reporterInterface["start"]; | |
var suiteStartFn = reporterInterface["suite-start"]; | |
var testStartFn = reporterInterface["test-start"]; | |
var testEndFn = reporterInterface["test-end"]; | |
var suiteEndFn = reporterInterface["suite-end"]; | |
var runEndFn = reporterInterface["end"]; | |
var reporterCtor = function(runner, options) { | |
if (typeof runStartFn === "function") { | |
runner.on("start", runStartFn); | |
} | |
if (typeof suiteStartFn === "function") { | |
runner.on("suite", suiteStartFn); | |
} | |
if (typeof testStartFn === "function") { | |
runner.on("test", testStartFn); | |
} | |
if (typeof testEndFn === "function") { | |
runner.on("test end", testEndFn); | |
} | |
if (typeof suiteEndFn === "function") { | |
runner.on("suite end", suiteEndFn); | |
} | |
if (typeof runEndFn === "function") { | |
runner.on("end", runEndFn); | |
} | |
}; | |
return reporterCtor; | |
} |
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
// QUnit usage | |
addQUnitReporter(QUnit, reporterInterface); | |
// Utility method | |
function addQUnitReporter(QUnit, reporterInterface) { | |
if (!(QUnit && reporterInterface)) { | |
return; | |
} | |
var runStartFn = reporterInterface["start"]; | |
var suiteStartFn = reporterInterface["suite-start"]; | |
var testStartFn = reporterInterface["test-start"]; | |
var assertionFn = reporterInterface["assert"]; | |
var testEndFn = reporterInterface["test-end"]; | |
var suiteEndFn = reporterInterface["suite-end"]; | |
var runEndFn = reporterInterface["end"]; | |
if (typeof runStartFn === "function") { | |
QUnit.begin(runStartFn); | |
} | |
if (typeof suiteStartFn === "function") { | |
QUnit.moduleStart(suiteStartFn); | |
} | |
if (typeof testStartFn === "function") { | |
QUnit.testStart(testStartFn); | |
} | |
// | |
// AFAIK, QUnit is currently the only JS test framework that supports assertion reporting | |
// | |
if (typeof assertionFn === "function") { | |
QUnit.log(assertionFn); | |
} | |
if (typeof testEndFn === "function") { | |
QUnit.testDone(testEndFn); | |
} | |
if (typeof suiteEndFn === "function") { | |
QUnit.moduleDone(suiteEndFn); | |
} | |
if (typeof runEndFn === "function") { | |
QUnit.done(runEndFn); | |
} | |
} |
@leobalter Isn't TAP a report output format than a reporter interface? I could be mistaken, haven't used it myself.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll add TAP just for consideration, as it can be plugable in the reporter API.
References: