Skip to content

Commit

Permalink
HTML Reporter: Show diff only when it helps
Browse files Browse the repository at this point in the history
Don't show diff if expected and actual values are completely different
and there is nothing in common.

Also hide the diff when a actual or expected value is a boolean.

Fixes #335
Closes #802
  • Loading branch information
shivamdixit authored and jzaefferer committed May 15, 2015
1 parent 60ca4b1 commit dd9d477
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions reporter/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,15 @@ QUnit.testStart(function( details ) {

});

function stripHtml( string ) {
// strip tags, html entity and whitespaces
return string.replace(/<\/?[^>]+(>|$)/g, "").replace(/\&quot;/g, "").replace(/\s+/g, "");
}

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

if ( !testItem ) {
Expand All @@ -659,19 +665,31 @@ QUnit.log(function( details ) {
"</pre></td></tr>";

if ( actual !== expected ) {

message += "<tr class='test-actual'><th>Result: </th><td><pre>" +
actual + "</pre></td></tr>" +
"<tr class='test-diff'><th>Diff: </th><td><pre>" +
QUnit.diff( expected, actual ) + "</pre></td></tr>";
} else {
if ( expected.indexOf( "[object Array]" ) !== -1 ||
expected.indexOf( "[object Object]" ) !== -1 ) {
message += "<tr class='test-message'><th>Message: </th><td>" +
"Diff suppressed as the depth of object is more than current max depth (" +
QUnit.config.maxDepth + ").<p>Hint: Use <code>QUnit.dump.maxDepth</code> to " +
" run with a higher max depth or <a href='" + setUrl({ maxDepth: -1 }) + "'>" +
"Rerun</a> without max depth.</p></td></tr>";
actual + "</pre></td></tr>";

// Don't show diff if actual or expected are booleans
if ( !( /^(true|false)$/.test( actual ) ) &&
!( /^(true|false)$/.test( expected ) ) ) {
diff = QUnit.diff( expected, actual );
showDiff = stripHtml( diff ).length !==
stripHtml( expected ).length +
stripHtml( actual ).length;
}

// Don't show diff if expected and actual are totally different
if ( showDiff ) {
message += "<tr class='test-diff'><th>Diff: </th><td><pre>" +
diff + "</pre></td></tr>";
}
} else if ( expected.indexOf( "[object Array]" ) !== -1 ||
expected.indexOf( "[object Object]" ) !== -1 ) {
message += "<tr class='test-message'><th>Message: </th><td>" +
"Diff suppressed as the depth of object is more than current max depth (" +
QUnit.config.maxDepth + ").<p>Hint: Use <code>QUnit.dump.maxDepth</code> to " +
" run with a higher max depth or <a href='" + setUrl({ maxDepth: -1 }) + "'>" +
"Rerun</a> without max depth.</p></td></tr>";
}

if ( details.source ) {
Expand Down

0 comments on commit dd9d477

Please # to comment.