Skip to content

Commit

Permalink
Core: Detail modules and tests names in the logging callbacks
Browse files Browse the repository at this point in the history
QUnit.begin now send the callback parameter with the modules
property, which now is an Array of objects each one with a
name property representing the module name and a tests property
with an Array of its respective tests objects with their
respective names and ids.

moduleDone and moduleStart also send in the callback parameter a
tests property containing an Array of its respective tests names.

config.moduleFilter is now removed to use only urlParams.module.

The HTML Reporter will logs tests since from the begin callback
listing the tests from there, this prevents logging them by
their execution, as they should be listed on their given order.

Fixes #674
Fixes #677
  • Loading branch information
leobalter authored and jzaefferer committed Dec 1, 2014
1 parent 36238ab commit 168b048
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 100 deletions.
95 changes: 63 additions & 32 deletions reporter/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,17 @@ function toolbarUrlConfigContainer() {
}

function getModuleNames() {
var i, l, name,
var i, l, name, ai, al,
moduleNames = [];

for ( i = 0, l = config.modules.length; i < l; i++ ) {
name = config.modules[ i ].name;

// Push only unique names
if ( moduleNames.indexOf( name ) < 0 ) {
moduleNames.push( name );
for ( ai = 0, al = moduleNames.length; ai < al; i++ ) {
if ( moduleNames[ ai ] === name ) {
moduleNames.push( name );
break;
}
}
}

Expand All @@ -307,13 +309,13 @@ function toolbarModuleFilterHtml() {

moduleFilterHtml += "<label for='qunit-modulefilter'>Module: </label>" +
"<select id='qunit-modulefilter' name='modulefilter'><option value='' " +
( config.moduleFilter === undefined ? "selected='selected'" : "" ) +
( QUnit.urlParams.module === undefined ? "selected='selected'" : "" ) +
">< All Modules ></option>";

for ( i = 0; i < moduleNames.length; i++ ) {
moduleFilterHtml += "<option value='" +
escapeText( encodeURIComponent( moduleNames[ i ] ) ) + "' " +
( config.moduleFilter === moduleNames[ i ] ? "selected='selected'" : "" ) +
( QUnit.urlParams.module === moduleNames[ i ] ? "selected='selected'" : "" ) +
">" + escapeText( moduleNames[ i ] ) + "</option>";
}
moduleFilterHtml += "</select>";
Expand Down Expand Up @@ -405,8 +407,50 @@ function appendUserAgent() {
}
}

function appendTestsList( modules ) {
var i, l, x, z, test, moduleObj;

for ( i = 0, l = modules.length; i < l; i++ ) {
moduleObj = modules[ i ];

for ( x = 0, z = moduleObj.tests.length; x < z; x++ ) {
test = moduleObj.tests[ x ];

appendTest( test.name, test.testId, moduleObj.name );
}
}
}

function appendTest( name, testId, moduleName ) {
var title, rerunTrigger, testBlock, assertList,
tests = id( "qunit-tests" );

if ( !tests ) {
return;
}

title = document.createElement( "strong" );
title.innerHTML = getNameHtml( name, moduleName );

rerunTrigger = document.createElement( "a" );
rerunTrigger.innerHTML = "Rerun";
rerunTrigger.href = QUnit.url({ testId: testId });

testBlock = document.createElement( "li" );
testBlock.appendChild( title );
testBlock.appendChild( rerunTrigger );
testBlock.id = "qunit-test-output-" + testId;

assertList = document.createElement( "ol" );
assertList.className = "qunit-assert-list";

testBlock.appendChild( assertList );

tests.appendChild( testBlock );
}

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

if ( qunit ) {
Expand All @@ -422,6 +466,7 @@ QUnit.begin(function() {
appendTestResults();
appendUserAgent();
appendToolbar();
appendTestsList( details.modules );
storeFixture();

if ( qunit && config.hidepassed ) {
Expand Down Expand Up @@ -493,43 +538,28 @@ function getNameHtml( name, module ) {
}

QUnit.testStart(function( details ) {
var a, b, li, running, assertList,
name = getNameHtml( details.name, details.module ),
tests = id( "qunit-tests" );
var running, testBlock;

if ( tests ) {
b = document.createElement( "strong" );
b.innerHTML = name;

a = document.createElement( "a" );
a.innerHTML = "Rerun";
a.href = QUnit.url({ testId: details.testId });

li = document.createElement( "li" );
li.appendChild( b );
li.appendChild( a );
li.className = "running";
li.id = "qunit-test-output" + details.testId;

assertList = document.createElement( "ol" );
assertList.className = "qunit-assert-list";

li.appendChild( assertList );
testBlock = id( "qunit-test-output-" + details.testId );
if ( testBlock ) {
testBlock.className = "running";
} else {

tests.appendChild( li );
// Report later registered tests
appendTest( details.name, details.testId, details.module );
}

running = id( "qunit-testresult" );
if ( running ) {
running.innerHTML = "Running: <br />" + name;
running.innerHTML = "Running: <br />" + getNameHtml( details.name, details.module );
}

});

QUnit.log(function( details ) {
var assertList, assertLi,
message, expected, actual,
testItem = id( "qunit-test-output" + details.testId );
testItem = id( "qunit-test-output-" + details.testId );

if ( !testItem ) {
return;
Expand Down Expand Up @@ -588,7 +618,8 @@ QUnit.testDone(function( details ) {
return;
}

testItem = id( "qunit-test-output" + details.testId );
testItem = id( "qunit-test-output-" + details.testId );

assertList = testItem.getElementsByTagName( "ol" )[ 0 ];

good = details.passed;
Expand Down
102 changes: 57 additions & 45 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,18 @@ config = {
// Set of all modules.
modules: [],

// The first unnamed module
currentModule: {
name: "",
tests: []
},

callbacks: {}
};

// Push a loose unnamed module to the modules collection
config.modules.push( config.currentModule );

// Initialize more QUnit.config and QUnit.urlParams
(function() {
var i, current,
Expand Down Expand Up @@ -163,9 +172,6 @@ config = {
// String search anywhere in moduleName+testName
config.filter = urlParams.filter;

// Exact match of the module name
config.moduleFilter = urlParams.module;

config.testId = [];
if ( urlParams.testId ) {

Expand All @@ -186,7 +192,11 @@ extend( QUnit, {

// call on start of module test to prepend name to all tests
module: function( name, testEnvironment ) {
var currentModule;
var currentModule = {
name: name,
testEnvironment: testEnvironment,
tests: []
};

// DEPRECATED: handles setup/teardown functions,
// beforeEach and afterEach should be used instead
Expand All @@ -199,12 +209,6 @@ extend( QUnit, {
delete testEnvironment.teardown;
}

currentModule = {
name: name,
testEnvironment: testEnvironment,
tests: []
};

config.modules.push( currentModule );
config.currentModule = currentModule;
},
Expand Down Expand Up @@ -457,12 +461,15 @@ window.onerror = function( error, filePath, linerNr ) {
};

function done() {
var runtime, passed;

config.autorun = true;

// Log the last module results
if ( config.previousModule ) {
runLoggingCallbacks( "moduleDone", {
name: config.previousModule.name,
tests: config.previousModule.tests,
failed: config.moduleStats.bad,
passed: config.moduleStats.all - config.moduleStats.bad,
total: config.moduleStats.all,
Expand All @@ -471,8 +478,8 @@ function done() {
}
delete config.previousModule;

var runtime = now() - config.started,
passed = config.stats.all - config.stats.bad;
runtime = now() - config.started;
passed = config.stats.all - config.stats.bad;

runLoggingCallbacks( "done", {
failed: config.stats.bad,
Expand Down Expand Up @@ -580,6 +587,42 @@ function process( last ) {
}
}

function begin() {
var i, l,
modulesLog = [];

// If the test run hasn't officially begun yet
if ( !config.started ) {

// Record the time of the test run's beginning
config.started = now();

verifyLoggingCallbacks();

// Delete the loose unnamed module if unused.
if ( config.modules[ 0 ].name === "" && config.modules[ 0 ].tests.length === 0 ) {
config.modules.shift();
}

// Avoid unnecessary information by not logging modules' test environments
for ( i = 0, l = config.modules.length; i < l; i++ ) {
modulesLog.push({
name: config.modules[ i ].name,
tests: config.modules[ i ].tests
});
}

// The test run is officially beginning now
runLoggingCallbacks( "begin", {
totalTests: Test.count,
modules: modulesLog
});
}

config.blocking = false;
process( true );
}

function resumeProcessing() {
runStarted = true;

Expand All @@ -593,41 +636,10 @@ function resumeProcessing() {
clearTimeout( config.timeout );
}

// If the test run hasn't officially begun yet
if ( !config.started ) {

// Record the time of the test run's beginning
config.started = now();

verifyLoggingCallbacks();

// The test run is officially beginning now
runLoggingCallbacks( "begin", {
totalTests: Test.count
});
}

config.blocking = false;
process( true );
begin();
}, 13 );
} else {

// If the test run hasn't officially begun yet
if ( !config.started ) {

// Record the time of the test run's beginning
config.started = now();

verifyLoggingCallbacks();

// The test run is officially beginning now
runLoggingCallbacks( "begin", {
totalTests: Test.count
});
}

config.blocking = false;
process( true );
begin();
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/qunit.css
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@
list-style-position: inside;
}

#qunit-tests > li {
display: none;
}

#qunit-tests li.pass, #qunit-tests li.running, #qunit-tests li.fail {
display: list-item;
}

#qunit-tests.hidepass li.pass, #qunit-tests.hidepass li.running {
display: none;
}
Expand Down
Loading

0 comments on commit 168b048

Please sign in to comment.