Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.
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: {
}
*/
}
};
// 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;
}
// 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;
}
// 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

This comment has been minimized.

Copy link

commented Mar 26, 2014

I'll add TAP just for consideration, as it can be plugable in the reporter API.

References:

@JamesMGreene

This comment has been minimized.

Copy link
Owner Author

commented Mar 30, 2014

@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
You can’t perform that action at this time.