@@ -2559,7 +2559,8 @@ function HTML(runner) {
2559
2559
}
2560
2560
2561
2561
// pass toggle
2562
- on ( passesLink , 'click' , function ( ) {
2562
+ on ( passesLink , 'click' , function ( evt ) {
2563
+ evt . preventDefault ( ) ;
2563
2564
unhide ( ) ;
2564
2565
var name = ( / p a s s / ) . test ( report . className ) ? '' : ' pass' ;
2565
2566
report . className = report . className . replace ( / f a i l | p a s s / g, '' ) + name ;
@@ -2569,7 +2570,8 @@ function HTML(runner) {
2569
2570
} ) ;
2570
2571
2571
2572
// failure toggle
2572
- on ( failuresLink , 'click' , function ( ) {
2573
+ on ( failuresLink , 'click' , function ( evt ) {
2574
+ evt . preventDefault ( ) ;
2573
2575
unhide ( ) ;
2574
2576
var name = ( / f a i l / ) . test ( report . className ) ? '' : ' fail' ;
2575
2577
report . className = report . className . replace ( / f a i l | p a s s / g, '' ) + name ;
@@ -2607,88 +2609,82 @@ function HTML(runner) {
2607
2609
stack . shift ( ) ;
2608
2610
} ) ;
2609
2611
2610
- runner . on ( 'fail' , function ( test ) {
2611
- // For type = 'test' its possible that the test failed due to multiple
2612
- // done() calls. So report the issue here.
2613
- if ( test . type === 'hook'
2614
- || test . type === 'test' ) {
2615
- runner . emit ( 'test end' , test ) ;
2616
- }
2612
+ runner . on ( 'pass' , function ( test ) {
2613
+ var url = self . testURL ( test ) ;
2614
+ var markup = '<li class="test pass %e"><h2>%e<span class="duration">%ems</span> '
2615
+ + '<a href="%s" class="replay">‣</a></h2></li>' ;
2616
+ var el = fragment ( markup , test . speed , test . title , test . duration , url ) ;
2617
+ self . addCodeToggle ( el , test . body ) ;
2618
+ appendToStack ( el ) ;
2619
+ updateStats ( ) ;
2617
2620
} ) ;
2618
2621
2619
- runner . on ( 'test end' , function ( test ) {
2620
- // TODO: add to stats
2621
- var percent = stats . tests / this . total * 100 | 0 ;
2622
- if ( progress ) {
2623
- progress . update ( percent ) . draw ( ctx ) ;
2624
- }
2625
-
2626
- // update stats
2627
- var ms = new Date ( ) - stats . start ;
2628
- text ( passes , stats . passes ) ;
2629
- text ( failures , stats . failures ) ;
2630
- text ( duration , ( ms / 1000 ) . toFixed ( 2 ) ) ;
2631
-
2632
- // test
2633
- var el ;
2634
- if ( test . state === 'passed' ) {
2635
- var url = self . testURL ( test ) ;
2636
- el = fragment ( '<li class="test pass %e"><h2>%e<span class="duration">%ems</span> <a href="%s" class="replay">‣</a></h2></li>' , test . speed , test . title , test . duration , url ) ;
2637
- } else if ( test . isPending ( ) ) {
2638
- el = fragment ( '<li class="test pass pending"><h2>%e</h2></li>' , test . title ) ;
2639
- } else {
2640
- el = fragment ( '<li class="test fail"><h2>%e <a href="%e" class="replay">‣</a></h2></li>' , test . title , self . testURL ( test ) ) ;
2641
- var stackString ; // Note: Includes leading newline
2642
- var message = test . err . toString ( ) ;
2643
-
2644
- // <=IE7 stringifies to [Object Error]. Since it can be overloaded, we
2645
- // check for the result of the stringifying.
2646
- if ( message === '[object Error]' ) {
2647
- message = test . err . message ;
2648
- }
2649
-
2650
- if ( test . err . stack ) {
2651
- var indexOfMessage = test . err . stack . indexOf ( test . err . message ) ;
2652
- if ( indexOfMessage === - 1 ) {
2653
- stackString = test . err . stack ;
2654
- } else {
2655
- stackString = test . err . stack . substr ( test . err . message . length + indexOfMessage ) ;
2656
- }
2657
- } else if ( test . err . sourceURL && test . err . line !== undefined ) {
2658
- // Safari doesn't give you a stack. Let's at least provide a source line.
2659
- stackString = '\n(' + test . err . sourceURL + ':' + test . err . line + ')' ;
2660
- }
2622
+ runner . on ( 'fail' , function ( test ) {
2623
+ var el = fragment ( '<li class="test fail"><h2>%e <a href="%e" class="replay">‣</a></h2></li>' ,
2624
+ test . title , self . testURL ( test ) ) ;
2625
+ var stackString ; // Note: Includes leading newline
2626
+ var message = test . err . toString ( ) ;
2661
2627
2662
- stackString = stackString || '' ;
2628
+ // <=IE7 stringifies to [Object Error]. Since it can be overloaded, we
2629
+ // check for the result of the stringifying.
2630
+ if ( message === '[object Error]' ) {
2631
+ message = test . err . message ;
2632
+ }
2663
2633
2664
- if ( test . err . htmlMessage && stackString ) {
2665
- el . appendChild ( fragment ( '<div class="html-error">%s\n<pre class="error">%e</pre></div>' , test . err . htmlMessage , stackString ) ) ;
2666
- } else if ( test . err . htmlMessage ) {
2667
- el . appendChild ( fragment ( '<div class="html-error">%s</div>' , test . err . htmlMessage ) ) ;
2634
+ if ( test . err . stack ) {
2635
+ var indexOfMessage = test . err . stack . indexOf ( test . err . message ) ;
2636
+ if ( indexOfMessage === - 1 ) {
2637
+ stackString = test . err . stack ;
2668
2638
} else {
2669
- el . appendChild ( fragment ( '<pre class="error">%e%e</pre>' , message , stackString ) ) ;
2639
+ stackString = test . err . stack . substr ( test . err . message . length + indexOfMessage ) ;
2670
2640
}
2641
+ } else if ( test . err . sourceURL && test . err . line !== undefined ) {
2642
+ // Safari doesn't give you a stack. Let's at least provide a source line.
2643
+ stackString = '\n(' + test . err . sourceURL + ':' + test . err . line + ')' ;
2671
2644
}
2672
2645
2673
- // toggle code
2674
- // TODO: defer
2675
- if ( ! test . isPending ( ) ) {
2676
- var h2 = el . getElementsByTagName ( 'h2' ) [ 0 ] ;
2646
+ stackString = stackString || '' ;
2677
2647
2678
- on ( h2 , 'click' , function ( ) {
2679
- pre . style . display = pre . style . display === 'none' ? 'block' : 'none' ;
2680
- } ) ;
2681
-
2682
- var pre = fragment ( '<pre><code>%e</code></pre> ' , utils . clean ( test . body ) ) ;
2683
- el . appendChild ( pre ) ;
2684
- pre . style . display = 'none' ;
2648
+ if ( test . err . htmlMessage && stackString ) {
2649
+ el . appendChild ( fragment ( '<div class="html-error">%s\n< pre class="error">%e</pre></div>' ,
2650
+ test . err . htmlMessage , stackString ) ) ;
2651
+ } else if ( test . err . htmlMessage ) {
2652
+ el . appendChild ( fragment ( '<div class="html-error">%s</div> ' , test . err . htmlMessage ) ) ;
2653
+ } else {
2654
+ el . appendChild ( fragment ( '<pre class="error">%e%e</pre>' , message , stackString ) ) ;
2685
2655
}
2686
2656
2657
+ self . addCodeToggle ( el , test . body ) ;
2658
+ appendToStack ( el ) ;
2659
+ updateStats ( ) ;
2660
+ } ) ;
2661
+
2662
+ runner . on ( 'pending' , function ( test ) {
2663
+ var el = fragment ( '<li class="test pass pending"><h2>%e</h2></li>' , test . title ) ;
2664
+ appendToStack ( el ) ;
2665
+ updateStats ( ) ;
2666
+ } ) ;
2667
+
2668
+ function appendToStack ( el ) {
2687
2669
// Don't call .appendChild if #mocha-report was already .shift()'ed off the stack.
2688
2670
if ( stack [ 0 ] ) {
2689
2671
stack [ 0 ] . appendChild ( el ) ;
2690
2672
}
2691
- } ) ;
2673
+ }
2674
+
2675
+ function updateStats ( ) {
2676
+ // TODO: add to stats
2677
+ var percent = stats . tests / this . total * 100 | 0 ;
2678
+ if ( progress ) {
2679
+ progress . update ( percent ) . draw ( ctx ) ;
2680
+ }
2681
+
2682
+ // update stats
2683
+ var ms = new Date ( ) - stats . start ;
2684
+ text ( passes , stats . passes ) ;
2685
+ text ( failures , stats . failures ) ;
2686
+ text ( duration , ( ms / 1000 ) . toFixed ( 2 ) ) ;
2687
+ }
2692
2688
}
2693
2689
2694
2690
/**
@@ -2726,6 +2722,24 @@ HTML.prototype.testURL = function(test) {
2726
2722
return makeUrl ( test . fullTitle ( ) ) ;
2727
2723
} ;
2728
2724
2725
+ /**
2726
+ * Adds code toggle functionality for the provided test's list element.
2727
+ *
2728
+ * @param {HTMLLIElement } el
2729
+ * @param {string } contents
2730
+ */
2731
+ HTML . prototype . addCodeToggle = function ( el , contents ) {
2732
+ var h2 = el . getElementsByTagName ( 'h2' ) [ 0 ] ;
2733
+
2734
+ on ( h2 , 'click' , function ( ) {
2735
+ pre . style . display = pre . style . display === 'none' ? 'block' : 'none' ;
2736
+ } ) ;
2737
+
2738
+ var pre = fragment ( '<pre><code>%e</code></pre>' , utils . clean ( contents ) ) ;
2739
+ el . appendChild ( pre ) ;
2740
+ pre . style . display = 'none' ;
2741
+ } ;
2742
+
2729
2743
/**
2730
2744
* Display error `msg`.
2731
2745
*
0 commit comments