File tree 5 files changed +54
-7
lines changed
5 files changed +54
-7
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
+ var prettyMs = require ( 'pretty-ms' ) ;
2
3
var chalk = require ( 'chalk' ) ;
3
4
var figures = require ( 'figures' ) ;
4
5
var Squeak = require ( 'squeak' ) ;
@@ -17,13 +18,27 @@ log.type('error', {
17
18
prefix : figures . cross
18
19
} ) ;
19
20
20
- function test ( err , title ) {
21
+ function test ( err , title , duration ) {
21
22
if ( err ) {
22
23
log . error ( title , chalk . red ( err . message ) ) ;
23
24
return ;
24
25
}
25
26
26
- log . success ( title ) ;
27
+ // format a time spent in the test
28
+ var timeSpent = '' ;
29
+
30
+ // display duration only over a threshold
31
+ var threshold = 100 ;
32
+
33
+ if ( duration > threshold ) {
34
+ var formattedDuration = prettyMs ( duration , {
35
+ secDecimalDigits : 2
36
+ } ) ;
37
+
38
+ timeSpent = chalk . gray . dim ( ' (' + formattedDuration + ')' ) ;
39
+ }
40
+
41
+ log . success ( title + timeSpent ) ;
27
42
}
28
43
29
44
function stack ( results ) {
Original file line number Diff line number Diff line change @@ -36,35 +36,37 @@ Runner.prototype.addSerialTest = function (title, cb) {
36
36
37
37
Runner . prototype . concurrent = function ( cb ) {
38
38
each ( this . tests . concurrent , function ( test , i , next ) {
39
- test . run ( function ( err ) {
39
+ test . run ( function ( err , duration ) {
40
40
if ( err ) {
41
41
this . stats . failCount ++ ;
42
42
}
43
43
44
44
this . results . push ( {
45
+ duration : duration ,
45
46
title : test . title ,
46
47
error : err
47
48
} ) ;
48
49
49
- this . emit ( 'test' , err , test . title ) ;
50
+ this . emit ( 'test' , err , test . title , duration ) ;
50
51
next ( ) ;
51
52
} . bind ( this ) ) ;
52
53
} . bind ( this ) , cb ) ;
53
54
} ;
54
55
55
56
Runner . prototype . serial = function ( cb ) {
56
57
eachSerial ( this . tests . serial , function ( test , next ) {
57
- test . run ( function ( err ) {
58
+ test . run ( function ( err , duration ) {
58
59
if ( err ) {
59
60
this . stats . failCount ++ ;
60
61
}
61
62
62
63
this . results . push ( {
64
+ duration : duration ,
63
65
title : test . title ,
64
66
error : err
65
67
} ) ;
66
68
67
- this . emit ( 'test' , err , test . title ) ;
69
+ this . emit ( 'test' , err , test . title , duration ) ;
68
70
next ( ) ;
69
71
} . bind ( this ) ) ;
70
72
} . bind ( this ) , cb ) ;
Original file line number Diff line number Diff line change @@ -21,6 +21,12 @@ function Test(title, fn) {
21
21
this . fn = fn ;
22
22
this . assertCount = 0 ;
23
23
this . planCount = null ;
24
+ this . duration = null ;
25
+
26
+ // store the time point
27
+ // before test execution
28
+ // to calculate the total time spent in test
29
+ this . _timeStart = null ;
24
30
}
25
31
26
32
util . inherits ( Test , EventEmitter ) ;
@@ -67,6 +73,8 @@ Test.prototype.run = function (cb) {
67
73
this . exit ( ) ;
68
74
}
69
75
76
+ this . _timeStart = Date . now ( ) ;
77
+
70
78
try {
71
79
var ret = this . fn ( this ) ;
72
80
@@ -98,6 +106,9 @@ Test.prototype.end = function () {
98
106
} ;
99
107
100
108
Test . prototype . exit = function ( ) {
109
+ // calculate total time spent in test
110
+ this . duration = Date . now ( ) - this . _timeStart ;
111
+
101
112
if ( this . planCount !== null && this . planCount !== this . assertCount ) {
102
113
this . assertError = new assert . AssertionError ( {
103
114
actual : this . assertCount ,
@@ -113,7 +124,7 @@ Test.prototype.exit = function () {
113
124
this . ended = true ;
114
125
115
126
setImmediate ( function ( ) {
116
- this . cb ( this . assertError ) ;
127
+ this . cb ( this . assertError , this . duration ) ;
117
128
} . bind ( this ) ) ;
118
129
}
119
130
} ;
Original file line number Diff line number Diff line change 54
54
"globby" : " ^2.0.0" ,
55
55
"meow" : " ^3.3.0" ,
56
56
"plur" : " ^2.0.0" ,
57
+ "pretty-ms" : " ^2.0.0" ,
57
58
"squeak" : " ^1.2.0" ,
58
59
"update-notifier" : " ^0.5.0"
59
60
},
Original file line number Diff line number Diff line change @@ -343,3 +343,21 @@ test('promise support - reject', function (t) {
343
343
t . end ( ) ;
344
344
} ) ;
345
345
} ) ;
346
+
347
+ test ( 'record test duration' , function ( t ) {
348
+ var avaTest ;
349
+
350
+ ava ( function ( a ) {
351
+ avaTest = a ;
352
+
353
+ a . plan ( 1 ) ;
354
+
355
+ setTimeout ( function ( ) {
356
+ a . true ( true ) ;
357
+ } , 1234 ) ;
358
+ } ) . run ( function ( err ) {
359
+ t . false ( err ) ;
360
+ t . true ( avaTest . duration >= 1234 ) ;
361
+ t . end ( ) ;
362
+ } ) ;
363
+ } ) ;
You can’t perform that action at this time.
0 commit comments