@@ -57,22 +57,22 @@ test('should sendMetrics and register when metricsInterval is a positive number'
5757 const url = getUrl ( ) ;
5858 const regEP = nockRegister ( url ) ;
5959 const metricsEP = nockMetrics ( url ) ;
60+ t . plan ( 2 ) ;
6061 // @ts -expect-error
6162 const metrics = new Metrics ( {
6263 url,
6364 metricsInterval : 50 ,
6465 } ) ;
6566
66- let metricsSent = 0 ;
67- let registered = 0 ;
68- metrics . on ( 'registered' , ( ) => {
69- registered ++ ;
70- t . true ( regEP . isDone ( ) ) ;
71- } ) ;
72- metrics . on ( 'sent' , ( ) => {
73- t . true ( metricsEP . isDone ( ) ) ;
74- metrics . stop ( ) ;
75- metricsSent ++ ;
67+ const validator = new Promise < void > ( ( resolve ) => {
68+ metrics . on ( 'registered' , ( ) => {
69+ t . true ( regEP . isDone ( ) ) ;
70+ } ) ;
71+ metrics . on ( 'sent' , ( ) => {
72+ t . true ( metricsEP . isDone ( ) ) ;
73+ metrics . stop ( ) ;
74+ resolve ( ) ;
75+ } ) ;
7676 } ) ;
7777
7878 metrics . count ( 'toggle-x' , true ) ;
@@ -81,18 +81,18 @@ test('should sendMetrics and register when metricsInterval is a positive number'
8181 metrics . start ( ) ;
8282 const timeout = new Promise < void > ( ( resolve ) =>
8383 setTimeout ( ( ) => {
84+ t . fail ( 'Failed to successfully both send and register' ) ;
8485 resolve ( ) ;
8586 } , 1000 ) ,
8687 ) ;
87- await timeout ;
88- t . is ( registered , 1 ) ;
89- t . is ( metricsSent , 1 ) ;
88+ await Promise . race ( [ validator , timeout ] ) ;
9089} ) ;
9190
9291test ( 'should sendMetrics' , async ( t ) => {
9392 const url = getUrl ( ) ;
93+ t . plan ( 7 ) ;
9494 const metricsEP = nock ( url )
95- . post ( metricsUrl , ( payload : any ) => {
95+ . post ( metricsUrl , ( payload ) => {
9696 t . truthy ( payload . bucket ) ;
9797 t . truthy ( payload . bucket . start ) ;
9898 t . truthy ( payload . bucket . stop ) ;
@@ -101,8 +101,6 @@ test('should sendMetrics', async (t) => {
101101 'toggle-y' : { yes : 1 , no : 0 , variants : { } } ,
102102 } ) ;
103103 t . deepEqual ( payload . connectionId , 'connection-id' ) ;
104- t . truthy ( payload . bucket . toggles [ 'toggle-x' ] ) ;
105- t . truthy ( payload . bucket . toggles [ 'toggle-y' ] ) ;
106104 return true ;
107105 } )
108106 . reply ( 200 , '' ) ;
@@ -130,6 +128,7 @@ test('should sendMetrics', async (t) => {
130128test ( 'should send correct custom and unleash headers' , ( t ) =>
131129 new Promise ( ( resolve ) => {
132130 const url = getUrl ( ) ;
131+ t . plan ( 2 ) ;
133132 const randomKey = `value-${ Math . random ( ) } ` ;
134133 const metricsEP = nockMetrics ( url )
135134 . matchHeader ( 'randomKey' , randomKey )
@@ -169,6 +168,7 @@ test('should send correct custom and unleash headers', (t) =>
169168
170169test ( 'should send content-type header' , async ( t ) => {
171170 const url = getUrl ( ) ;
171+ t . plan ( 2 ) ;
172172 const metricsEP = nockMetrics ( url ) . matchHeader ( 'content-type' , 'application/json' ) ;
173173 const regEP = nockRegister ( url ) . matchHeader ( 'content-type' , 'application/json' ) ;
174174
@@ -188,14 +188,15 @@ test('should send content-type header', async (t) => {
188188
189189test ( 'request with customHeadersFunction should take precedence over customHeaders' , async ( t ) => {
190190 const url = getUrl ( ) ;
191+ t . plan ( 2 ) ;
191192 const customHeadersKey = `value-${ Math . random ( ) } ` ;
192193 const randomKey = `value-${ Math . random ( ) } ` ;
193194 const metricsEP = nockMetrics ( url )
194- . matchHeader ( 'randomKey' , ( value : any ) => value === undefined )
195+ . matchHeader ( 'randomKey' , ( value ) => value === undefined )
195196 . matchHeader ( 'customHeadersKey' , customHeadersKey ) ;
196197
197198 const regEP = nockRegister ( url )
198- . matchHeader ( 'randomKey' , ( value : any ) => value === undefined )
199+ . matchHeader ( 'randomKey' , ( value ) => value === undefined )
199200 . matchHeader ( 'customHeadersKey' , customHeadersKey ) ;
200201
201202 // @ts -expect-error
@@ -221,7 +222,10 @@ test.skip('should respect timeout', (t) =>
221222 new Promise ( ( resolve , reject ) => {
222223 t . plan ( 2 ) ;
223224 const url = getUrl ( ) ;
225+ // @ts -expect-error
224226 nock ( url ) . post ( metricsUrl ) . socketDelay ( 100 ) . reply ( 200 , '' ) ;
227+
228+ // @ts -expect-error
225229 nock ( url ) . post ( registerUrl ) . socketDelay ( 100 ) . reply ( 200 , '' ) ;
226230
227231 // @ts -expect-error
@@ -242,6 +246,7 @@ test.skip('should respect timeout', (t) =>
242246 } ) ) ;
243247
244248test ( 'registerInstance should warn when non 200 statusCode' , async ( t ) => {
249+ t . plan ( 2 ) ;
245250 const url = getUrl ( ) ;
246251 const regEP = nockRegister ( url , 500 ) ;
247252
@@ -649,7 +654,7 @@ test('sendMetrics should include impactMetrics in the payload', async (t) => {
649654 } ) ;
650655
651656 const scope = nock ( url )
652- . post ( '/client/metrics' , ( body : any ) => {
657+ . post ( '/client/metrics' , ( body ) => {
653658 capturedBody = body ;
654659 return true ;
655660 } )
@@ -712,7 +717,7 @@ test('sendMetrics should not include impactMetrics field when empty', async (t)
712717 metrics . count ( 'toggle-x' , true ) ;
713718
714719 const scope = nock ( url )
715- . post ( '/client/metrics' , ( body : any ) => {
720+ . post ( '/client/metrics' , ( body ) => {
716721 capturedBody = body ;
717722 return true ;
718723 } )
0 commit comments