Output the details of failed tests from qUnit when run via PhantomJs on the command line

_________________________________________________
Problem:
When running your qUnit javascript tests from PhantomJs, the default script run-qunit.js (shipped with PhantomJs) does not output the details of the tests that failed.

This is not critical, since you can run the tests manually in the browser, by opening the test HTML file.

However, it is a bit annoying, and can be a problem if the qUnit settings are different in the tests, when run from PhantomJs, as compared to when you run the tests manually in the browser.

note: the qUnit settings can be specified as query params after the URL to the test HTML page.
example: to turn ON the global leak detection:

"MapModelTests.html?noglobals=true"
_________________________________________________
Solution: 
modify run-qunit.js in order to output the details of the failed tests

here is a modification you can make to run-qunit.js in order to show details of the tests that failed:

         } catch (e) { }  
         return 10000;  
       });  
       //existing code up to this point !  
       //BEGIN - output the details of the failed tests:  
       page.evaluate(function () {  
         var el = document.getElementById('qunit-tests');  
         //console.log(el.innerText);  
         try {  
           //console.log(document.documentElement.outerHTML); //handy to see the full test output  
           var failHtmls = el.getElementsByClassName('fail');  
           var testsOutput = []; //prevent duplicate test output  
           for (var failHtml in failHtmls) {  
             failHtml = failHtmls[failHtml];  
             if (!failHtml)  
               continue;  
             console.log('----');  
             var isDuplicate = false;  
             var testMessage = failHtml.getElementsByClassName('test-message');  
             if (testMessage) {  
               if (testMessage[0]) {  
                 testMessage = testMessage[0];  
                 for (var existingMessage in testsOutput) {  
                   existingMessage = testsOutput[existingMessage];  
                   if (existingMessage == testMessage.innerHTML) {  
                     isDuplicate = true;  
                   }  
                 }  
                 if (!isDuplicate) {  
                   testsOutput.push(testMessage.innerHTML);  
                   console.log('test FAILED: ' + testMessage.innerHTML);  
                 }  
               }  
             }  
             if (!isDuplicate) {  
               var testSource = failHtml.getElementsByClassName('test-source');  
               if (testSource) {  
                 if (testSource[0]) {  
                   console.log('test FAILED source: ' + testSource[0].innerHTML);  
                 }  
               }  
             }  
           }  
         } catch (e) {  
           //console.log('!!! run-qunit.js exception - ' + e);  
         }  
       });  
       //END - output the details of the failed tests:  
       //existing code starts again:  
       phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0);  

Comments