44 */
55
66'use strict'
7-
8- const tap = require ( 'tap ' )
7+ const assert = require ( 'node:assert' )
8+ const test = require ( 'node:test ' )
99const sinon = require ( 'sinon' )
1010const loggingUtils = require ( '../../../lib/util/application-logging' )
1111const { LOGGING } = require ( '../../../lib/metrics/names' )
1212
13- tap . test ( 'truncate' , ( t ) => {
14- t . autoend ( )
15- t . test ( 'Should truncate string > 1024 chars' , ( t ) => {
13+ test ( 'truncate' , async ( t ) => {
14+ await t . test ( 'Should truncate string > 1024 chars' , ( ) => {
1615 const longString =
1716 '1111111111111111111111111111111111111111111111111111111111111111' +
1817 '1111111111111111111111111111111111111111111111111111111111111111' +
@@ -35,19 +34,16 @@ tap.test('truncate', (t) => {
3534
3635 const processedStr = loggingUtils . truncate ( longString )
3736
38- t . equal ( processedStr . length , 1024 )
39- t . equal ( processedStr . substring ( processedStr . length - 3 ) , '...' )
40-
41- t . end ( )
37+ assert . equal ( processedStr . length , 1024 )
38+ assert . equal ( processedStr . substring ( processedStr . length - 3 ) , '...' )
4239 } )
4340
44- t . test ( 'Should return non-truncated string when <= 1024 chars' , ( t ) => {
41+ await t . test ( 'Should return non-truncated string when <= 1024 chars' , ( ) => {
4542 const str = 'kenny loggins'
4643
4744 const processedStr = loggingUtils . truncate ( str )
4845
49- t . equal ( processedStr , str )
50- t . end ( )
46+ assert . equal ( processedStr , str )
5147 } )
5248
5349 const negativeTests = [
@@ -58,27 +54,25 @@ tap.test('truncate', (t) => {
5854 { value : [ ] , type : 'array' } ,
5955 { value : function ( ) { } , type : 'function' }
6056 ]
61- negativeTests . forEach ( ( { value, type } ) => {
62- t . test ( `should not truncate ${ type } ` , ( t ) => {
57+ for ( const negativeTest of negativeTests ) {
58+ const { value, type } = negativeTest
59+ await t . test ( `should not truncate ${ type } ` , ( ) => {
6360 const newValue = loggingUtils . truncate ( value )
64- t . same ( value , newValue )
65- t . end ( )
61+ assert . deepEqual ( value , newValue )
6662 } )
67- } )
63+ }
6864} )
6965
70- tap . test ( 'Application Logging Config Tests' , ( t ) => {
71- t . autoend ( )
66+ test ( 'Application Logging Config Tests' , async ( t ) => {
7267 const features = [
7368 { feature : 'metrics' , method : 'isMetricsEnabled' } ,
7469 { feature : 'forwarding' , method : 'isLogForwardingEnabled' } ,
7570 { feature : 'local_decorating' , method : 'isLocalDecoratingEnabled' }
7671 ]
7772
78- let config = { }
79-
80- t . beforeEach ( ( ) => {
81- config = {
73+ t . beforeEach ( ( ctx ) => {
74+ ctx . nr = { }
75+ ctx . nr . config = {
8276 application_logging : {
8377 enabled : true ,
8478 metrics : {
@@ -94,88 +88,90 @@ tap.test('Application Logging Config Tests', (t) => {
9488 }
9589 } )
9690
97- features . forEach ( ( { feature, method } ) => {
98- t . test (
99- `isApplicationLoggingEnabled should be true when application_logging and ${ feature } is truthy` ,
100- ( t ) => {
101- config . application_logging [ feature ] . enabled = true
102- t . equal ( loggingUtils . isApplicationLoggingEnabled ( config ) , true )
103- t . end ( )
104- }
105- )
91+ await Promise . all (
92+ features . map ( async ( { feature, method } ) => {
93+ await t . test (
94+ `isApplicationLoggingEnabled should be true when application_logging and ${ feature } is truthy` ,
95+ ( t ) => {
96+ const { config } = t . nr
97+ config . application_logging [ feature ] . enabled = true
98+ assert . equal ( loggingUtils . isApplicationLoggingEnabled ( config ) , true )
99+ }
100+ )
106101
107- t . test ( `${ method } should be true when application_logging and ${ feature } are truthy` , ( t ) => {
108- config . application_logging [ feature ] . enabled = true
109- if ( feature === 'forwarding' ) {
110- t . equal ( loggingUtils [ method ] ( config , { logs : true } ) , true )
111- } else {
112- t . equal ( loggingUtils [ method ] ( config ) , true )
113- }
114- t . end ( )
102+ await t . test (
103+ `${ method } should be true when application_logging and ${ feature } are truthy` ,
104+ ( t ) => {
105+ const { config } = t . nr
106+ config . application_logging [ feature ] . enabled = true
107+ if ( feature === 'forwarding' ) {
108+ assert . equal ( loggingUtils [ method ] ( config , { logs : true } ) , true )
109+ } else {
110+ assert . equal ( loggingUtils [ method ] ( config ) , true )
111+ }
112+ }
113+ )
115114 } )
116- } )
115+ )
117116
118- t . test ( 'should be false when application_logging is false' , ( t ) => {
117+ await t . test ( 'should be false when application_logging is false' , ( t ) => {
118+ const { config } = t . nr
119119 config . application_logging . enabled = false
120- t . equal ( loggingUtils . isApplicationLoggingEnabled ( config ) , false )
121- t . end ( )
120+ assert . equal ( loggingUtils . isApplicationLoggingEnabled ( config ) , false )
122121 } )
123122
124- t . test ( 'should be false when all features are false' , ( t ) => {
125- t . equal ( loggingUtils . isApplicationLoggingEnabled ( config ) , false )
126- t . end ( )
123+ await t . test ( 'should be false when all features are false' , ( t ) => {
124+ const { config } = t . nr
125+ assert . equal ( loggingUtils . isApplicationLoggingEnabled ( config ) , false )
127126 } )
128127} )
129128
130- tap . test ( 'incrementLoggingLinesMetrics' , ( t ) => {
131- t . autoend ( )
132- let callCountStub = null
133- let metricsStub = null
134- t . beforeEach ( ( ) => {
135- callCountStub = { incrementCallCount : sinon . stub ( ) }
136- metricsStub = {
129+ test ( 'incrementLoggingLinesMetrics' , async ( t ) => {
130+ t . beforeEach ( ( ctx ) => {
131+ console . log ( 'before test' )
132+ ctx . nr = { }
133+ const callCountStub = { incrementCallCount : sinon . stub ( ) }
134+ ctx . nr . metricsStub = {
137135 getOrCreateMetric : sinon . stub ( ) . returns ( callCountStub )
138136 }
139- } )
140-
141- t . afterEach ( ( ) => {
142- callCountStub = null
143- metricsStub = null
137+ ctx . nr . callCountStub = callCountStub
144138 } )
145139
146140 const levels = Object . keys ( LOGGING . LEVELS )
147- levels . forEach ( ( level ) => {
148- const levelLowercase = level . toLowerCase ( )
149- t . test ( `should increment logging lines metrics for level: ${ levelLowercase } ` , ( t ) => {
150- loggingUtils . incrementLoggingLinesMetrics ( levelLowercase , metricsStub )
151- t . equal (
152- metricsStub . getOrCreateMetric . args [ 0 ] [ 0 ] ,
153- LOGGING . LINES ,
154- `should create ${ LOGGING . LINES } metric`
155- )
156- t . equal (
157- metricsStub . getOrCreateMetric . args [ 1 ] [ 0 ] ,
158- LOGGING . LEVELS [ level ] ,
159- `should create ${ LOGGING . LEVELS [ level ] } metric`
160- )
161- t . equal ( callCountStub . incrementCallCount . callCount , 2 , 'should increment each metric' )
162- t . end ( )
141+ await Promise . all (
142+ levels . map ( async ( level ) => {
143+ const levelLowercase = level . toLowerCase ( )
144+ await t . test ( `should increment logging lines metrics for level: ${ levelLowercase } ` , ( t ) => {
145+ const { metricsStub, callCountStub } = t . nr
146+ loggingUtils . incrementLoggingLinesMetrics ( levelLowercase , metricsStub )
147+ assert . equal (
148+ metricsStub . getOrCreateMetric . args [ 0 ] [ 0 ] ,
149+ LOGGING . LINES ,
150+ `should create ${ LOGGING . LINES } metric`
151+ )
152+ assert . equal (
153+ metricsStub . getOrCreateMetric . args [ 1 ] [ 0 ] ,
154+ LOGGING . LEVELS [ level ] ,
155+ `should create ${ LOGGING . LEVELS [ level ] } metric`
156+ )
157+ assert . equal ( callCountStub . incrementCallCount . callCount , 2 , 'should increment each metric' )
158+ } )
163159 } )
164- } )
160+ )
165161
166- t . test ( 'should default to unknown when level is undefined' , ( t ) => {
162+ await t . test ( 'should default to unknown when level is undefined' , ( t ) => {
163+ const { metricsStub, callCountStub } = t . nr
167164 loggingUtils . incrementLoggingLinesMetrics ( undefined , metricsStub )
168- t . equal (
165+ assert . equal (
169166 metricsStub . getOrCreateMetric . args [ 0 ] [ 0 ] ,
170167 LOGGING . LINES ,
171168 `should create ${ LOGGING . LINES } metric`
172169 )
173- t . equal (
170+ assert . equal (
174171 metricsStub . getOrCreateMetric . args [ 1 ] [ 0 ] ,
175172 LOGGING . LEVELS . UNKNOWN ,
176173 `should create ${ LOGGING . LEVELS . UNKNOWN } metric`
177174 )
178- t . equal ( callCountStub . incrementCallCount . callCount , 2 , 'should increment each metric' )
179- t . end ( )
175+ assert . equal ( callCountStub . incrementCallCount . callCount , 2 , 'should increment each metric' )
180176 } )
181177} )
0 commit comments