44import { formatDuration } from '../core/utility/Util.js' ;
55
66describe ( 'Format duration tests' , ( ) => {
7- it ( 'Shows µs for very small values' , ( ) => {
8- expect ( formatDuration ( 5 ) ) . toBe ( '0.01 µs' ) ;
9- expect ( formatDuration ( 50 ) ) . toBe ( '0.05 µs' ) ;
10- expect ( formatDuration ( 500 ) ) . toBe ( '0.5 µs' ) ;
11- expect ( formatDuration ( 1000 ) ) . toBe ( '1 µs' ) ;
12- expect ( formatDuration ( 5000 ) ) . toBe ( '5 µs' ) ;
13- expect ( formatDuration ( 9999 ) ) . toBe ( '10 µs' ) ;
14- expect ( formatDuration ( 10000 ) ) . toBe ( '10 µs' ) ;
7+ it ( 'Shows ms with decimals for very small values (sub-millisecond)' , ( ) => {
8+ expect ( formatDuration ( 5 ) ) . toBe ( '0 ms' ) ; // 0.000005 ms rounds to 0
9+ expect ( formatDuration ( 50 ) ) . toBe ( '0 ms' ) ; // 0.00005 ms rounds to 0
10+ expect ( formatDuration ( 500 ) ) . toBe ( '0.001 ms' ) ;
11+ expect ( formatDuration ( 1000 ) ) . toBe ( '0.001 ms' ) ;
12+ expect ( formatDuration ( 5000 ) ) . toBe ( '0.005 ms' ) ;
13+ expect ( formatDuration ( 9999 ) ) . toBe ( '0.01 ms' ) ;
14+ expect ( formatDuration ( 10000 ) ) . toBe ( '0.01 ms' ) ;
15+ expect ( formatDuration ( 50000 ) ) . toBe ( '0.05 ms' ) ;
16+ expect ( formatDuration ( 99999 ) ) . toBe ( '0.1 ms' ) ;
1517 } ) ;
1618
1719 it ( 'handles ms duration' , ( ) => {
1820 expect ( formatDuration ( 100_000 ) ) . toBe ( '0.1 ms' ) ;
21+ expect ( formatDuration ( 500_000 ) ) . toBe ( '0.5 ms' ) ;
1922 expect ( formatDuration ( 1_000_000 ) ) . toBe ( '1 ms' ) ;
2023 expect ( formatDuration ( 1_234_567 ) ) . toBe ( '1.23 ms' ) ;
2124 expect ( formatDuration ( 9_999_999 ) ) . toBe ( '10 ms' ) ;
@@ -41,22 +44,22 @@ describe('Format duration tests', () => {
4144 } ) ;
4245
4346 it ( 'handles remove trailing 0 for all units types' , ( ) => {
44- expect ( formatDuration ( 5000 ) ) . toBe ( '5 µs ' ) ;
47+ expect ( formatDuration ( 5000 ) ) . toBe ( '0.005 ms ' ) ;
4548 expect ( formatDuration ( 100_000 ) ) . toBe ( '0.1 ms' ) ;
4649 expect ( formatDuration ( 5_000_000_000 ) ) . toBe ( '5 s' ) ;
4750 expect ( formatDuration ( 60_000_000_000 ) ) . toBe ( '1m' ) ;
4851 } ) ;
4952
50- it ( 'handles rounding to 2dp for µs, ms, s ' , ( ) => {
51- // microseconds
52- expect ( formatDuration ( 1234 ) ) . toBe ( '1.23 µs ' ) ;
53- expect ( formatDuration ( 9876 ) ) . toBe ( '9.88 µs ' ) ;
53+ it ( 'handles rounding to appropriate precision ' , ( ) => {
54+ // sub-milliseconds (up to 3 decimal places)
55+ expect ( formatDuration ( 1234 ) ) . toBe ( '0.001 ms ' ) ;
56+ expect ( formatDuration ( 9876 ) ) . toBe ( '0.01 ms ' ) ;
5457
55- // milliseconds
58+ // milliseconds (up to 2 decimal places)
5659 expect ( formatDuration ( 1_234_567 ) ) . toBe ( '1.23 ms' ) ;
5760 expect ( formatDuration ( 9_876_543 ) ) . toBe ( '9.88 ms' ) ;
5861
59- // seconds
62+ // seconds (up to 2 decimal places)
6063 expect ( formatDuration ( 1_234_567_890 ) ) . toBe ( '1.23 s' ) ;
6164 expect ( formatDuration ( 9_876_543_210 ) ) . toBe ( '9.88 s' ) ;
6265 } ) ;
@@ -65,4 +68,25 @@ describe('Format duration tests', () => {
6568 // minutes with fractional seconds
6669 expect ( formatDuration ( 125_670_000_000 ) ) . toBe ( '2m 5.7s' ) ;
6770 } ) ;
71+
72+ describe ( 'compact option' , ( ) => {
73+ it ( 'omits spaces for milliseconds' , ( ) => {
74+ expect ( formatDuration ( 0 , { compact : true } ) ) . toBe ( '0ms' ) ;
75+ expect ( formatDuration ( 50000 , { compact : true } ) ) . toBe ( '0.05ms' ) ;
76+ expect ( formatDuration ( 1_000_000 , { compact : true } ) ) . toBe ( '1ms' ) ;
77+ expect ( formatDuration ( 1_234_567 , { compact : true } ) ) . toBe ( '1.23ms' ) ;
78+ expect ( formatDuration ( 100_000_000 , { compact : true } ) ) . toBe ( '100ms' ) ;
79+ } ) ;
80+
81+ it ( 'omits spaces for seconds' , ( ) => {
82+ expect ( formatDuration ( 5_000_000_000 , { compact : true } ) ) . toBe ( '5s' ) ;
83+ expect ( formatDuration ( 59_500_000_000 , { compact : true } ) ) . toBe ( '59.5s' ) ;
84+ } ) ;
85+
86+ it ( 'omits spaces for minutes' , ( ) => {
87+ expect ( formatDuration ( 60_000_000_000 , { compact : true } ) ) . toBe ( '1m' ) ;
88+ expect ( formatDuration ( 125_000_000_000 , { compact : true } ) ) . toBe ( '2m5s' ) ;
89+ expect ( formatDuration ( 125_500_000_000 , { compact : true } ) ) . toBe ( '2m5.5s' ) ;
90+ } ) ;
91+ } ) ;
6892} ) ;
0 commit comments