From 477e1f103da06c219a59c992e0611e32886a9fa1 Mon Sep 17 00:00:00 2001 From: Gaurav Mittal Date: Thu, 5 Mar 2015 01:10:25 +0530 Subject: [PATCH] Test: Source Displayed Even for Passed Test Closes #758 --- reporter/html.js | 16 ++++++++++++++- src/test.js | 3 +++ test/logs.js | 7 +++++++ test/reporter-html/reporter-html.js | 31 +++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/reporter/html.js b/reporter/html.js index 7b5d52345..927ba4b2b 100644 --- a/reporter/html.js +++ b/reporter/html.js @@ -699,7 +699,7 @@ QUnit.log(function( details ) { QUnit.testDone(function( details ) { var testTitle, time, testItem, assertList, - good, bad, testCounts, skipped, + good, bad, testCounts, skipped, sourceName, tests = id( "qunit-tests" ); if ( !tests ) { @@ -754,6 +754,20 @@ QUnit.testDone(function( details ) { time.innerHTML = details.runtime + " ms"; testItem.insertBefore( time, assertList ); } + + // Only add this information if stackTrace is supported + if ( details.source ) { + sourceName = document.createElement( "p" ); + sourceName.innerHTML = "Source: " + details.source; + addClass( sourceName, "qunit-source" ); + if ( bad === 0 ) { + addClass( sourceName, "qunit-collapsed" ); + } + addEvent( testTitle, "click", function() { + toggleClass( sourceName, "qunit-collapsed" ); + }); + testItem.appendChild( sourceName ); + } }); if ( !defined.document || document.readyState === "complete" ) { diff --git a/src/test.js b/src/test.js index faf89567d..03879e619 100644 --- a/src/test.js +++ b/src/test.js @@ -202,6 +202,9 @@ Test.prototype = { assertions: this.assertions, testId: this.testId, + // Source of Test + source: this.stack, + // DEPRECATED: this property will be removed in 2.0.0, use runtime instead duration: this.runtime }); diff --git a/test/logs.js b/test/logs.js index 76c251ac5..18d238120 100644 --- a/test/logs.js +++ b/test/logs.js @@ -175,10 +175,16 @@ QUnit.test( module1Test2.name, function( assert ) { // TODO: more tests for testDoneContext.assertions delete testDoneContext.runtime; + // DEPRECATED: remove this delete when removing the duration property delete testDoneContext.duration; + // Delete testDoneContext.assertions so we can easily jump to next assertion delete testDoneContext.assertions; + + // Delete testDoneContext.source + delete testDoneContext.source; + assert.deepEqual( testDoneContext, { module: module1Context.name, name: module1Test1.name, @@ -259,6 +265,7 @@ QUnit.test( module2Test4.name, function( assert ) { delete testDoneContext.runtime; delete testDoneContext.duration; + delete testDoneContext.source; assert.deepEqual( testDoneContext, { assertions: [], diff --git a/test/reporter-html/reporter-html.js b/test/reporter-html/reporter-html.js index 9efdf251a..f5408a686 100644 --- a/test/reporter-html/reporter-html.js +++ b/test/reporter-html/reporter-html.js @@ -95,3 +95,34 @@ QUnit.test( "values", function( assert ) { "Runtime includes beforeEach" ); }); + +QUnit.module( "source" ); + +QUnit.test( "setup", function( assert ) { + assert.expect( 0 ); +}); + +QUnit.test( "logs location", function( assert ) { + var previous = document.getElementById( "qunit-test-output-" + assert.test.testId ) + .previousSibling; + var source = previous.lastChild; + var stack = QUnit.stack(); + + // Verify QUnit supported stack trace + if ( !stack ) { + assert.equal( + /(^| )qunit-source( |$)/.test( source.className ), + false, + "Don't add source information on non-supported environments" + ); + return; + } + + assert.ok( /(^| )qunit-source( |$)/.test( source.className ), "Source element exists" ); + assert.equal( source.firstChild.innerHTML, "Source: " ); + + // test/reporter-html.js is a direct reference to this test file + assert.ok( /\/test\/reporter-html\/reporter-html\.js\:\d+/.test( source.innerHTML ), + "Source references to the current file and line number" + ); +});