11const { createMockInstance } = require ( 'jest-create-mock-instance' ) ;
2+ const formatDate = require ( 'date-fns/format' ) ;
23const Logger = require ( '../logger' ) ;
34const LogTimings = require ( './log-timings' ) ;
45const createFakeCommand = require ( './fixtures/fake-command' ) ;
@@ -9,17 +10,17 @@ const startDate1 = new Date(startDate0.getTime() + 1000);
910const endDate1 = new Date ( startDate0 . getTime ( ) + 3000 ) ;
1011const endDate0 = new Date ( startDate0 . getTime ( ) + 5000 ) ;
1112
12- const getDurationText = ( startDate , endDate ) => ( endDate . getTime ( ) - startDate . getTime ( ) ) . toLocaleString ( ) ;
13+ const timestampFormat = 'yyyy-MM-dd HH:mm:ss.SSS' ;
14+ const getDurationText = ( startDate , endDate ) => `${ ( endDate . getTime ( ) - startDate . getTime ( ) ) . toLocaleString ( ) } ms` ;
1315const command0DurationTextMs = getDurationText ( startDate0 , endDate0 ) ;
1416const command1DurationTextMs = getDurationText ( startDate1 , endDate1 ) ;
1517
16- const exitInfoToTimingInfo = ( { command, timings, index , killed, exitCode } ) => {
17- const readableDurationMs = ( timings . endDate - timings . startDate ) . toLocaleString ( ) ;
18+ const exitInfoToTimingInfo = ( { command, timings, killed, exitCode } ) => {
19+ const readableDurationMs = getDurationText ( timings . startDate , timings . endDate ) ;
1820 return {
19- 'call-index' : index ,
2021 name : command . name ,
21- duration : `~ ${ readableDurationMs } ms` ,
22- 'exit- code' : exitCode ,
22+ duration : readableDurationMs ,
23+ 'exit code' : exitCode ,
2324 killed,
2425 command : command . command ,
2526 } ;
@@ -56,7 +57,7 @@ beforeEach(() => {
5657 } ;
5758
5859 logger = createMockInstance ( Logger ) ;
59- controller = new LogTimings ( { logger } ) ;
60+ controller = new LogTimings ( { logger, timestampFormat } ) ;
6061} ) ;
6162
6263it ( 'returns same commands' , ( ) => {
@@ -69,8 +70,8 @@ it('does not log timings and doesn\'t throw if no logger is provided', () => {
6970
7071 commands [ 0 ] . timer . next ( { startDate : startDate0 } ) ;
7172 commands [ 1 ] . timer . next ( { startDate : startDate1 } ) ;
72- commands [ 1 ] . timer . next ( { startDate : startDate1 , endDate : endDate1 } ) ;
73- commands [ 0 ] . timer . next ( { startDate : startDate0 , endDate : endDate0 } ) ;
73+ commands [ 1 ] . timer . next ( { startDate : startDate1 , endDate : endDate1 } ) ;
74+ commands [ 0 ] . timer . next ( { startDate : startDate0 , endDate : endDate0 } ) ;
7475
7576 expect ( logger . logCommandEvent ) . toHaveBeenCalledTimes ( 0 ) ;
7677} ) ;
@@ -80,24 +81,24 @@ it('logs the timings at the start and end (ie complete or error) event of each c
8081
8182 commands [ 0 ] . timer . next ( { startDate : startDate0 } ) ;
8283 commands [ 1 ] . timer . next ( { startDate : startDate1 } ) ;
83- commands [ 1 ] . timer . next ( { startDate : startDate1 , endDate : endDate1 } ) ;
84- commands [ 0 ] . timer . next ( { startDate : startDate0 , endDate : endDate0 } ) ;
84+ commands [ 1 ] . timer . next ( { startDate : startDate1 , endDate : endDate1 } ) ;
85+ commands [ 0 ] . timer . next ( { startDate : startDate0 , endDate : endDate0 } ) ;
8586
8687 expect ( logger . logCommandEvent ) . toHaveBeenCalledTimes ( 4 ) ;
8788 expect ( logger . logCommandEvent ) . toHaveBeenCalledWith (
88- `${ commands [ 0 ] . command } started at ${ startDate0 . toLocaleString ( ) } ` ,
89+ `${ commands [ 0 ] . command } started at ${ formatDate ( startDate0 , timestampFormat ) } ` ,
8990 commands [ 0 ]
9091 ) ;
9192 expect ( logger . logCommandEvent ) . toHaveBeenCalledWith (
92- `${ commands [ 1 ] . command } started at ${ startDate1 . toLocaleString ( ) } ` ,
93+ `${ commands [ 1 ] . command } started at ${ formatDate ( startDate1 , timestampFormat ) } ` ,
9394 commands [ 1 ]
9495 ) ;
9596 expect ( logger . logCommandEvent ) . toHaveBeenCalledWith (
96- `${ commands [ 1 ] . command } stopped at ${ endDate1 . toLocaleString ( ) } after ${ command1DurationTextMs } ms ` ,
97+ `${ commands [ 1 ] . command } stopped at ${ formatDate ( endDate1 , timestampFormat ) } after ${ command1DurationTextMs } ` ,
9798 commands [ 1 ]
9899 ) ;
99100 expect ( logger . logCommandEvent ) . toHaveBeenCalledWith (
100- `${ commands [ 0 ] . command } stopped at ${ endDate0 . toLocaleString ( ) } after ${ command0DurationTextMs } ms ` ,
101+ `${ commands [ 0 ] . command } stopped at ${ formatDate ( endDate0 , timestampFormat ) } after ${ command0DurationTextMs } ` ,
101102 commands [ 0 ]
102103 ) ;
103104} ) ;
0 commit comments