Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events: QUnit logging system uses EventEmitter #644

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ grunt.initConfig({
"test/index.html",
"test/autostart.html",
"test/startError.html",
"test/events.html",
"test/logs.html",
"test/setTimeout.html"
]
Expand Down Expand Up @@ -173,21 +174,21 @@ grunt.registerTask( "test-on-node", function() {
done = this.async(),
QUnit = require( "./dist/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 @@ -204,7 +205,7 @@ grunt.registerTask( "test-on-node", function() {
});
QUnit.config.autorun = false;

require( "./test/logs" );
require( "./test/events" );
require( "./test/test" );
require( "./test/async" );
require( "./test/promise" );
Expand Down
1 change: 1 addition & 0 deletions browserstack.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"test_path": [
"test/index.html",
"test/startError.html",
"test/events.html",
"test/logs.html",
"test/setTimeout.html"
],
Expand Down
10 changes: 5 additions & 5 deletions reporter/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ function appendUserAgent() {
}

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

if ( qunit ) {
Expand All @@ -468,7 +468,7 @@ QUnit.begin(function() {
storeFixture();
});

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

QUnit.testStart(function( details ) {
QUnit.on( "testStart", function( details ) {
var a, b, li, running, assertList,
name = getNameHtml( details.name, details.module ),
tests = id( "qunit-tests" );
Expand Down Expand Up @@ -565,7 +565,7 @@ QUnit.testStart(function( details ) {

});

QUnit.log(function( details ) {
QUnit.on( "assert", function( details ) {
var assertList, assertLi,
message, expected, actual,
testItem = id( "qunit-test-output" + details.testId );
Expand Down Expand Up @@ -618,7 +618,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,
tests = id( "qunit-tests" );
Expand Down
90 changes: 54 additions & 36 deletions src/core.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var QUnit,
config,
onErrorFnPrev,
var QUnit, config, onErrorFnPrev,
fileName = ( sourceFromStacktrace( 0 ) || "" ).replace( /(:\d+)+\)?/, "" ).replace( /.+\//, "" ),
toString = Object.prototype.toString,
hasOwn = Object.prototype.hasOwnProperty,
Expand All @@ -13,6 +11,7 @@ var QUnit,
runStarted = false,
setTimeout = window.setTimeout,
clearTimeout = window.clearTimeout,
listeners = {},
defined = {
document: typeof window.document !== "undefined",
setTimeout: typeof window.setTimeout !== "undefined",
Expand Down Expand Up @@ -192,7 +191,28 @@ QUnit = {
config.current.semaphore += count || 1;

pauseProcessing();
},

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 );
}
}

};

// We use the prototype to distinguish between properties that should
Expand Down Expand Up @@ -362,35 +382,38 @@ extend( QUnit, {
});

/**
* @deprecated: Created for backwards compatibility with test runner that set the hook function
* DEPRECATED: Created for backwards compatibility with test runner that set the hook function
* into QUnit.{hook}, instead of invoking it and passing the hook function.
* QUnit.constructor is set to the empty F() above so that we can add to it's prototype here.
* Doing this allows us to tell if the following methods have been overwritten on the actual
* QUnit object.
*
* DEPRECATED: These logging callbacks will removed in QUnit 2.0. Use `QUnit.on()` instead.
*/
extend( QUnit.constructor.prototype, {

// Logging callbacks; all receive a single argument with the listed properties
// run test/logs.html for any related changes
begin: registerLoggingCallback( "begin" ),
begin: registerLoggingCallback( "runStart" ),

// done: { failed, passed, total, runtime }
done: registerLoggingCallback( "done" ),
done: registerLoggingCallback( "runEnd" ),

// log: { result, actual, expected, message, runtime }
log: registerLoggingCallback( "log" ),
log: registerLoggingCallback( "assert" ),

// testStart: { name }
testStart: registerLoggingCallback( "testStart" ),

// testDone: { name, failed, passed, total, runtime }
testDone: registerLoggingCallback( "testDone" ),
testDone: registerLoggingCallback( "testEnd" ),

// moduleStart: { name }
moduleStart: registerLoggingCallback( "moduleStart" ),
moduleStart: registerLoggingCallback( "suiteStart" ),

// moduleDone: { name, failed, passed, total, runtime }
moduleDone: registerLoggingCallback( "moduleDone" )
moduleDone: registerLoggingCallback( "suiteEnd" )

});

QUnit.load = function() {
Expand Down Expand Up @@ -445,12 +468,28 @@ window.onerror = function( error, filePath, linerNr ) {
return ret;
};

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 );
}
}
}

function done() {
config.autorun = true;

// Log the last module results
if ( config.previousModule ) {
runLoggingCallbacks( "moduleDone", {
emit( "suiteEnd", {
name: config.previousModule,
failed: config.moduleStats.bad,
passed: config.moduleStats.all - config.moduleStats.bad,
Expand All @@ -463,7 +502,7 @@ function done() {
var 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 Expand Up @@ -583,7 +622,7 @@ function resumeProcessing() {
config.started = now();

// The test run is officially beginning now
runLoggingCallbacks( "begin", {
emit( "runStart", {
totalTests: Test.count
});
}
Expand All @@ -600,7 +639,7 @@ function resumeProcessing() {
config.started = now();

// The test run is officially beginning now
runLoggingCallbacks( "begin", {
emit( "runStart", {
totalTests: Test.count
});
}
Expand Down Expand Up @@ -697,32 +736,11 @@ function extend( a, b, undefOnly ) {
}

function registerLoggingCallback( key ) {

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

return function( callback ) {
if ( QUnit.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( key, callback );
};
}

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

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

// from jquery.js
function inArray( elem, array ) {
if ( array.indexOf ) {
Expand Down
12 changes: 6 additions & 6 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Test.prototype = {
!hasOwn.call( config, "previousModule" )
) {
if ( hasOwn.call( config, "previousModule" ) ) {
runLoggingCallbacks( "moduleDone", {
emit( "suiteEnd", {
name: config.previousModule,
failed: config.moduleStats.bad,
passed: config.moduleStats.all - config.moduleStats.bad,
Expand All @@ -46,7 +46,7 @@ Test.prototype = {
}
config.previousModule = this.module;
config.moduleStats = { all: 0, bad: 0, started: now() };
runLoggingCallbacks( "moduleStart", {
emit( "suiteStart", {
name: this.module
});
}
Expand All @@ -58,7 +58,7 @@ Test.prototype = {
delete this.testEnvironment.afterEach;

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

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

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

this.assertions.push({
result: !!result,
Expand All @@ -281,7 +281,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