Skip to content

Commit

Permalink
Core: QUnit logging system uses EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMGreene authored and leobalter committed Oct 27, 2015
1 parent 67727bc commit ae371d5
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 47 deletions.
10 changes: 6 additions & 4 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ grunt.initConfig({
"test/index.html",
"test/autostart.html",
"test/startError.html",
"test/events.html",
"test/logs.html",
"test/setTimeout.html",
"test/amd.html",
Expand Down Expand Up @@ -161,21 +162,21 @@ grunt.registerTask( "test-on-node", function() {

global.QUnit = QUnit;

QUnit.testStart(function() {
QUnit.on( "testStart", function() {
testActive = true;
});
QUnit.log(function( details ) {
QUnit.on( "assert", function( details ) {
if ( !testActive || details.result ) {
return;
}
var message = "name: " + details.name + " module: " + details.module +
" message: " + details.message;
grunt.log.error( message );
});
QUnit.testDone(function() {
QUnit.on( "testEnd", function() {
testActive = false;
});
QUnit.done(function( details ) {
QUnit.on( "runEnd", function( details ) {
if ( runDone ) {
return;
}
Expand All @@ -200,6 +201,7 @@ grunt.registerTask( "test-on-node", function() {
require( "./test/main/modules" );
require( "./test/main/deepEqual" );
require( "./test/main/stack" );
require( "./test/events" );
require( "./test/globals-node" );

QUnit.load();
Expand Down
3 changes: 2 additions & 1 deletion build/browserstack-current-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"test_framework": "qunit",
"test_path": [
"test/index.html",
"test/logs.html"
"test/logs.html",
"test/events.html"
],
"browsers": [
"chrome_current",
Expand Down
3 changes: 2 additions & 1 deletion build/browserstack-legacy-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"test_framework": "qunit",
"test_path": [
"test/index.html",
"test/logs.html"
"test/logs.html",
"test/events.html"
],
"browsers": [
"chrome_previous",
Expand Down
10 changes: 5 additions & 5 deletions reporter/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ function appendTest( name, testId, moduleName ) {
}

// HTML Reporter initialization and load
QUnit.begin(function( details ) {
QUnit.on( "runStart", function( details ) {
var qunit = id( "qunit" );

// Fixture is the only one necessary to run without the #qunit element
Expand Down Expand Up @@ -560,7 +560,7 @@ QUnit.begin(function( details ) {
}
});

QUnit.done(function( details ) {
QUnit.on( "runEnd", function( details ) {
var i, key,
banner = id( "qunit-banner" ),
tests = id( "qunit-tests" ),
Expand Down Expand Up @@ -623,7 +623,7 @@ function getNameHtml( name, module ) {
return nameHtml;
}

QUnit.testStart(function( details ) {
QUnit.on( "testStart", function( details ) {
var running, testBlock, bad;

testBlock = id( "qunit-test-output-" + details.testId );
Expand Down Expand Up @@ -653,7 +653,7 @@ function stripHtml( string ) {
return string.replace(/<\/?[^>]+(>|$)/g, "").replace(/\&quot;/g, "").replace(/\s+/g, "");
}

QUnit.log(function( details ) {
QUnit.on( "assert", function( details ) {
var assertList, assertLi,
message, expected, actual, diff,
showDiff = false,
Expand Down Expand Up @@ -733,7 +733,7 @@ QUnit.log(function( details ) {
assertList.appendChild( assertLi );
});

QUnit.testDone(function( details ) {
QUnit.on( "testEnd", function( details ) {
var testTitle, time, testItem, assertList,
good, bad, testCounts, skipped, sourceName,
tests = id( "qunit-tests" );
Expand Down
6 changes: 3 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function begin() {
}

// The test run is officially beginning now
runLoggingCallbacks( "begin", {
emit( "runStart", {
totalTests: Test.count,
modules: modulesLog
});
Expand Down Expand Up @@ -301,7 +301,7 @@ function done() {

// Log the last module results
if ( config.previousModule ) {
runLoggingCallbacks( "moduleDone", {
emit( "suiteEnd", {
name: config.previousModule.name,
tests: config.previousModule.tests,
failed: config.moduleStats.bad,
Expand All @@ -315,7 +315,7 @@ function done() {
runtime = now() - config.started;
passed = config.stats.all - config.stats.bad;

runLoggingCallbacks( "done", {
emit( "runEnd", {
failed: config.stats.bad,
passed: passed,
total: config.stats.all,
Expand Down
83 changes: 56 additions & 27 deletions src/core/logging.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
var listeners = {};
var loggingCallbacks = {};

// Register logging callbacks
function registerLoggingCallbacks( obj ) {
var i, l, key,
callbackNames = [ "begin", "done", "log", "testStart", "testDone",
// DEPRECATED: Register old logging callbacks
// This will be removed on QUnit 2.0.0+
// From now on use the QUnit.on( eventType, callback ) format
function registerLoggingCallbacks( QUnit ) {
var i, l, key;
var callbackNames = [ "begin", "done", "log", "testStart", "testDone",
"moduleStart", "moduleDone" ];
var dictionary = {
"begin": "runStart",
"done": "runEnd",
"log": "assert",
"testStart": "testStart",
"testDone": "testEnd",
"moduleStart": "suiteStart",
"moduleDone": "suiteEnd"
};

function registerLoggingCallback( key ) {
var loggingCallback = function( callback ) {
if ( objectType( callback ) !== "function" ) {
throw new Error(
"QUnit logging methods require a callback function as their first parameters."
);
}

config.callbacks[ key ].push( callback );
return QUnit.on( dictionary[ key ], callback );
};

// DEPRECATED: This will be removed on QUnit 2.0.0+
// Stores the registered functions allowing restoring
// at verifyLoggingCallbacks() if modified
loggingCallbacks[ key ] = loggingCallback;
Expand All @@ -28,21 +33,7 @@ function registerLoggingCallbacks( obj ) {
for ( i = 0, l = callbackNames.length; i < l; i++ ) {
key = callbackNames[ i ];

// Initialize key collection of logging callback
if ( objectType( config.callbacks[ key ] ) === "undefined" ) {
config.callbacks[ key ] = [];
}

obj[ key ] = registerLoggingCallback( key );
}
}

function runLoggingCallbacks( key, args ) {
var i, l, callbacks;

callbacks = config.callbacks[ key ];
for ( i = 0, l = callbacks.length; i < l; i++ ) {
callbacks[ i ]( args );
QUnit[ key ] = registerLoggingCallback( key );
}
}

Expand Down Expand Up @@ -73,3 +64,41 @@ function verifyLoggingCallbacks() {
}
}
}

function emit( type, data ) {
var i, len, callbacks;

// Validate
if ( QUnit.objectType( type ) !== "string" ) {
throw new Error( "Emitting QUnit events requires an event type" );
}

callbacks = listeners[ type ];
if ( callbacks ) {
for ( i = 0, len = callbacks.length; i < len; i++ ) {
callbacks[ i ]( data );
}
}
}

QUnit.on = function( type, listener ) {

// Validate
if ( QUnit.objectType( type ) !== "string" ) {
throw new Error( "Adding QUnit events requires an event type" );
}

if ( QUnit.objectType( listener ) !== "function" ) {
throw new Error( "Adding QUnit events requires a listener function" );
}

// Initialize collection of this logging callback
if ( !listeners[ type ] ) {
listeners[ type ] = [];
}

// Filter out duplicate listeners
if ( inArray( listener, listeners[ type ] ) < 0 ) {
listeners[ type ].push( listener );
}
};
12 changes: 6 additions & 6 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Test.prototype = {
!hasOwn.call( config, "previousModule" )
) {
if ( hasOwn.call( config, "previousModule" ) ) {
runLoggingCallbacks( "moduleDone", {
emit( "suiteEnd", {
name: config.previousModule.name,
tests: config.previousModule.tests,
failed: config.moduleStats.bad,
Expand All @@ -64,7 +64,7 @@ Test.prototype = {
}
config.previousModule = this.module;
config.moduleStats = { all: 0, bad: 0, started: now() };
runLoggingCallbacks( "moduleStart", {
emit( "suiteStart", {
name: this.module.name,
tests: this.module.tests
});
Expand All @@ -79,7 +79,7 @@ Test.prototype = {
this.testEnvironment = extend( {}, this.module.testEnvironment );

this.started = now();
runLoggingCallbacks( "testStart", {
emit( "testStart", {
name: this.testName,
module: this.module.name,
testId: this.testId
Expand Down Expand Up @@ -203,7 +203,7 @@ Test.prototype = {
}
}

runLoggingCallbacks( "testDone", {
emit( "testEnd", {
name: this.testName,
module: this.module.name,
skipped: !!this.skip,
Expand Down Expand Up @@ -292,7 +292,7 @@ Test.prototype = {
}
}

runLoggingCallbacks( "log", details );
emit( "assert", details );

this.assertions.push({
result: !!result,
Expand Down Expand Up @@ -320,7 +320,7 @@ Test.prototype = {
details.source = source;
}

runLoggingCallbacks( "log", details );
emit( "assert", details );

this.assertions.push({
result: false,
Expand Down
16 changes: 16 additions & 0 deletions test/events.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QUnit Events Test Suite</title>
<link rel="stylesheet" href="../dist/qunit.css">
<script src="../dist/qunit.js"></script>
<script>
QUnit.config.reorder = false;
</script>
<script src="events.js"></script>
</head>
<body>
<div id="qunit"></div>
</body>
</html>
Loading

0 comments on commit ae371d5

Please sign in to comment.