Skip to content

Commit ce3da2a

Browse files
bizob2828sumitsuthar
authored andcommitted
test: Migrated test/unit/util to use node:test (newrelic#2546)
1 parent d65ae85 commit ce3da2a

13 files changed

+594
-596
lines changed

test/unit/util/application-logging.test.js

Lines changed: 77 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
*/
55

66
'use strict'
7-
8-
const tap = require('tap')
7+
const assert = require('node:assert')
8+
const test = require('node:test')
99
const sinon = require('sinon')
1010
const loggingUtils = require('../../../lib/util/application-logging')
1111
const { 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
})

test/unit/util/async-each-limit.test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
*/
55

66
'use strict'
7-
8-
const { test } = require('tap')
7+
const assert = require('node:assert')
8+
const test = require('node:test')
99
const sinon = require('sinon')
1010
const eachLimit = require('../../../lib/util/async-each-limit')
1111

12-
test('eachLimit should limit concurrent async executions', async (t) => {
12+
test('eachLimit should limit concurrent async executions', async () => {
1313
let firstPromiseResolve
1414
let secondPromiseResolve
1515
let thirdPromiseResolve
@@ -47,20 +47,20 @@ test('eachLimit should limit concurrent async executions', async (t) => {
4747

4848
const promise = eachLimit(items, mapper, 2)
4949

50-
t.equal(access.callCount, 2, 'should have called two promises')
51-
t.ok(access.calledWith('foo.json'), 'should have called the first promise')
52-
t.ok(access.calledWith('bar.json'), 'should have called the second promise')
53-
t.notOk(access.calledWith('baz.json'), 'should not have called the third promise yet')
50+
assert.equal(access.callCount, 2, 'should have called two promises')
51+
assert.ok(access.calledWith('foo.json'), 'should have called the first promise')
52+
assert.ok(access.calledWith('bar.json'), 'should have called the second promise')
53+
assert.ok(!access.calledWith('baz.json'), 'should not have called the third promise yet')
5454

5555
firstPromiseResolve()
56-
t.notOk(access.calledWith('baz.json'), 'should still not have called the third promise')
56+
assert.ok(!access.calledWith('baz.json'), 'should still not have called the third promise')
5757

5858
secondPromiseResolve()
5959
thirdPromiseResolve()
6060

6161
const results = await promise
6262

63-
t.equal(access.callCount, 3, 'should have called three promises')
64-
t.ok(access.calledWith('baz.json'), 'should have called the third promise')
65-
t.same(results, [true, true, true], 'should return the correct results')
63+
assert.equal(access.callCount, 3, 'should have called three promises')
64+
assert.ok(access.calledWith('baz.json'), 'should have called the third promise')
65+
assert.deepEqual(results, [true, true, true], 'should return the correct results')
6666
})

test/unit/util/byte-limit.test.js

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,65 @@
44
*/
55

66
'use strict'
7-
8-
const { test } = require('tap')
7+
const assert = require('node:assert')
8+
const test = require('node:test')
99
const byteUtils = require('../../../lib/util/byte-limit')
1010

11-
test('byte-limit', (t) => {
12-
t.autoend()
13-
14-
t.test('#isValidLength', (t) => {
15-
t.autoend()
16-
t.test('returns false when the string is larger than the limit', (t) => {
17-
t.notOk(byteUtils.isValidLength('12345', 4))
18-
t.end()
11+
test('byte-limit', async (t) => {
12+
await t.test('#isValidLength', async (t) => {
13+
await t.test('returns false when the string is larger than the limit', () => {
14+
assert.ok(!byteUtils.isValidLength('12345', 4))
1915
})
2016

21-
t.test('returns true when the string is equal to the limit', (t) => {
22-
t.ok(byteUtils.isValidLength('12345', 5))
23-
t.end()
17+
await t.test('returns true when the string is equal to the limit', () => {
18+
assert.ok(byteUtils.isValidLength('12345', 5))
2419
})
2520

26-
t.test('returns true when the string is smaller than the limit', (t) => {
27-
t.ok(byteUtils.isValidLength('12345', 6))
28-
t.end()
21+
await t.test('returns true when the string is smaller than the limit', () => {
22+
assert.ok(byteUtils.isValidLength('12345', 6))
2923
})
3024
})
31-
t.test('#compareLength', (t) => {
32-
t.autoend()
33-
t.test('returns -1 when the string is smaller than the limit', (t) => {
25+
26+
await t.test('#compareLength', async (t) => {
27+
await t.test('returns -1 when the string is smaller than the limit', () => {
3428
const str = '123456789'
3529
const cmpVal = byteUtils.compareLength(str, 255)
36-
t.ok(cmpVal < 0)
37-
t.end()
30+
assert.ok(cmpVal < 0)
3831
})
39-
t.test('returns 0 when the string is equal than the limit', (t) => {
32+
await t.test('returns 0 when the string is equal than the limit', () => {
4033
const str = '123456789'
4134
const cmpVal = byteUtils.compareLength(str, 9)
42-
t.equal(cmpVal, 0)
43-
t.end()
35+
assert.equal(cmpVal, 0)
4436
})
45-
t.test('returns 1 when the string is larger than the limit', (t) => {
37+
await t.test('returns 1 when the string is larger than the limit', () => {
4638
const str = '123456789'
4739
const cmpVal = byteUtils.compareLength(str, 2)
48-
t.ok(cmpVal > 0)
49-
t.end()
40+
assert.ok(cmpVal > 0)
5041
})
5142
})
5243

53-
t.test('#truncate', (t) => {
54-
t.autoend()
55-
t.test('truncates string value to given limit', (t) => {
44+
await t.test('#truncate', async (t) => {
45+
await t.test('truncates string value to given limit', () => {
5646
let str = '123456789'
5747
str = byteUtils.truncate(str, 5)
58-
t.equal(str, '12345')
59-
t.end()
48+
assert.equal(str, '12345')
6049
})
61-
t.test('returns original string if within limit', (t) => {
50+
await t.test('returns original string if within limit', () => {
6251
let str = '123456789'
6352
str = byteUtils.truncate(str, 10)
64-
t.equal(str, '123456789')
65-
t.end()
53+
assert.equal(str, '123456789')
6654
})
67-
t.test('respects multibyte characters', (t) => {
55+
await t.test('respects multibyte characters', () => {
6856
let str = '\uD87E\uDC04\uD87E\uDC04'
69-
t.equal(Buffer.byteLength(str, 'utf8'), 8)
57+
assert.equal(Buffer.byteLength(str, 'utf8'), 8)
7058
str = byteUtils.truncate(str, 3)
71-
t.equal(str, '\uD87E')
72-
t.end()
59+
assert.equal(str, '\uD87E')
7360
})
74-
t.test('should strings with split unicode characters properly', (t) => {
61+
await t.test('should strings with split unicode characters properly', () => {
7562
let str = '\uD87E\uDC04\uD87E\uDC04'
76-
t.equal(Buffer.byteLength(str, 'utf8'), 8)
63+
assert.equal(Buffer.byteLength(str, 'utf8'), 8)
7764
str = byteUtils.truncate(str, 2)
78-
t.equal(str, '')
79-
t.end()
65+
assert.equal(str, '')
8066
})
8167
})
8268
})

0 commit comments

Comments
 (0)