Skip to content

Commit

Permalink
chore: Migrate block of unit tests to node:test (newrelic#2607)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr authored Sep 24, 2024
1 parent bd2f1a5 commit e33807b
Show file tree
Hide file tree
Showing 11 changed files with 775 additions and 881 deletions.
2 changes: 0 additions & 2 deletions test/lib/temp-remove-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ module.exports = function tempRemoveListeners({ t, emitter, event }) {
return
}

t.diagnostic(`Removing listeners for ${event}`)
const listeners = emitter.listeners(event)
emitter.removeAllListeners(event)

// We probably shouldn't be adding a `t.after` in this helper. There can only
// be one `t.after` handler per test, and putting in here obscures the fact
// that it has been added.
t.after(() => {
t.diagnostic(`Re-adding listeners for ${event}`)
for (const l of listeners) {
emitter.on(event, l)
}
Expand Down
15 changes: 10 additions & 5 deletions test/unit/load-externals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@

'use strict'

const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')

const loadExternals = require('../../load-externals')

tap.test('should load libs to webpack externals', async (t) => {
test('should load libs to webpack externals', async () => {
const config = {
target: 'node-20.x',
externals: ['next']
}
loadExternals(config)
t.ok(config.externals.length > 1, 'should add all libraries agent supports to the externals list')
assert.ok(
config.externals.length > 1,
'should add all libraries agent supports to the externals list'
)
})

tap.test('should not add externals when target is not node', async (t) => {
test('should not add externals when target is not node', async () => {
const config = {
target: 'web',
externals: ['next']
}
loadExternals(config)
t.ok(config.externals.length === 1, 'should not agent libraries when target is not node')
assert.ok(config.externals.length === 1, 'should not agent libraries when target is not node')
})
239 changes: 120 additions & 119 deletions test/unit/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,154 +5,155 @@

'use strict'

const tap = require('tap')
const cp = require('child_process')
const test = require('node:test')
const assert = require('node:assert')
const path = require('node:path')
const cp = require('node:child_process')

const tempRemoveListeners = require('../lib/temp-remove-listeners')

const Logger = require('../../lib/util/logger')
const path = require('path')

tap.test('Logger', function (t) {
t.autoend()
let logger = null

t.beforeEach(function () {
logger = new Logger({
name: 'newrelic',
level: 'trace',
enabled: true,
configured: true
})
})

t.afterEach(function () {
logger = null
test.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.logger = new Logger({
name: 'newrelic',
level: 'trace',
enabled: true,
configured: true
})
})

t.test('should not throw when passed-in log level is 0', function (t) {
t.doesNotThrow(function () {
logger.level(0)
})
t.end()
test('should not throw when passed-in log level is 0', (t) => {
const { logger } = t.nr
assert.doesNotThrow(() => {
logger.level(0)
})
})

t.test('should not throw when passed-in log level is ONE MILLION', function (t) {
t.doesNotThrow(function () {
logger.level(1000000)
})
t.end()
test('should not throw when passed-in log level is ONE MILLION', (t) => {
const { logger } = t.nr
assert.doesNotThrow(function () {
logger.level(1000000)
})
})

t.test('should not throw when passed-in log level is "verbose"', function (t) {
t.doesNotThrow(function () {
logger.level('verbose')
})
t.end()
test('should not throw when passed-in log level is "verbose"', (t) => {
const { logger } = t.nr
assert.doesNotThrow(function () {
logger.level('verbose')
})
})

t.test('should enqueue logs until configured', function (t) {
logger.options.configured = false
logger.trace('trace')
logger.debug('debug')
logger.info('info')
logger.warn('warn')
logger.error('error')
logger.fatal('fatal')
t.ok(logger.logQueue.length === 6, 'should have 6 logs in the queue')
t.end()
})
test('should enqueue logs until configured', (t) => {
const { logger } = t.nr
logger.options.configured = false
logger.trace('trace')
logger.debug('debug')
logger.info('info')
logger.warn('warn')
logger.error('error')
logger.fatal('fatal')
assert.ok(logger.logQueue.length === 6, 'should have 6 logs in the queue')
})

t.test('should not enqueue logs when disabled', function (t) {
logger.trace('trace')
logger.debug('debug')
logger.info('info')
logger.warn('warn')
logger.error('error')
logger.fatal('fatal')
t.ok(logger.logQueue.length === 0, 'should have 0 logs in the queue')
t.end()
})
test('should not enqueue logs when disabled', (t) => {
const { logger } = t.nr
logger.trace('trace')
logger.debug('debug')
logger.info('info')
logger.warn('warn')
logger.error('error')
logger.fatal('fatal')
assert.ok(logger.logQueue.length === 0, 'should have 0 logs in the queue')
})

t.test('should flush logs when configured', function (t) {
logger.options.configured = false
logger.trace('trace')
logger.debug('debug')
logger.info('info')
logger.warn('warn')
logger.error('error')
logger.fatal('fatal')

t.ok(logger.logQueue.length === 6, 'should have 6 logs in the queue')

logger.configure({
level: 'trace',
enabled: true,
name: 'test-logger'
})
test('should flush logs when configured', (t) => {
const { logger } = t.nr
logger.options.configured = false
logger.trace('trace')
logger.debug('debug')
logger.info('info')
logger.warn('warn')
logger.error('error')
logger.fatal('fatal')

t.ok(logger.logQueue.length === 0, 'should have 0 logs in the queue')
t.end()
assert.ok(logger.logQueue.length === 6, 'should have 6 logs in the queue')

logger.configure({
level: 'trace',
enabled: true,
name: 'test-logger'
})

t.test('should fallback to default logging config when config is invalid', function (t) {
runTestFile('disabled-with-invalid-config/disabled.js', function (error, message) {
t.notOk(error)
assert.ok(logger.logQueue.length === 0, 'should have 0 logs in the queue')
})

test('should fallback to default logging config when config is invalid', (t, end) => {
runTestFile('disabled-with-invalid-config/disabled.js', function (error, message) {
assert.equal(error, undefined)

// should pipe logs to stdout if config is invalid, even if logging is disabled
t.ok(message)
t.end()
})
// should pipe logs to stdout if config is invalid, even if logging is disabled
assert.ok(message)
end()
})
})

t.test('should not cause crash if unwritable', function (t) {
runTestFile('unwritable-log/unwritable.js', t.end)
})
test('should not cause crash if unwritable', (t, end) => {
runTestFile('unwritable-log/unwritable.js', end)
})

t.test('should not be created if logger is disabled', function (t) {
runTestFile('disabled-log/disabled.js', t.end)
})
test('should not be created if logger is disabled', (t, end) => {
runTestFile('disabled-log/disabled.js', end)
})

t.test('should not log bootstrapping logs when logs disabled', function (t) {
runTestFile('disabled-with-log-queue/disabled.js', function (error, message) {
t.notOk(error)
t.notOk(message)
t.end()
})
test('should not log bootstrapping logs when logs disabled', (t, end) => {
runTestFile('disabled-with-log-queue/disabled.js', function (error, message) {
assert.equal(error, undefined)
assert.equal(message, undefined)
end()
})
})

t.test('should log bootstrapping logs at specified level when logs enabled', function (t) {
runTestFile('enabled-with-log-queue/enabled.js', function (error, message) {
t.notOk(error)
t.ok(message)
test('should log bootstrapping logs at specified level when logs enabled', (t, end) => {
runTestFile('enabled-with-log-queue/enabled.js', function (error, message) {
assert.equal(error, undefined)
assert.ok(message)

let logs = []
t.doesNotThrow(function () {
logs = message.split('\n').filter(Boolean).map(JSON.parse)
})
let logs = []
assert.doesNotThrow(function () {
logs = message.split('\n').filter(Boolean).map(JSON.parse)
})

t.ok(logs.length >= 1)
t.ok(logs.every((log) => log.level >= 30))
assert.ok(logs.length >= 1)
assert.ok(logs.every((log) => log.level >= 30))

t.end()
})
end()
})
})

t.test('should not throw for huge messages', function (t) {
process.once('warning', (warning) => {
t.equal(warning.name, 'NewRelicWarning')
t.ok(warning.message)
t.end()
})
test('should not throw for huge messages', (t, end) => {
const { logger } = t.nr

let huge = 'a'
while (huge.length < Logger.MAX_LOG_BUFFER / 2) {
huge += huge
}

t.doesNotThrow(() => {
logger.fatal('some message to start the buffer off')
logger.fatal(huge)
logger.fatal(huge)
})
tempRemoveListeners({ t, emitter: process, event: 'warning' })
process.once('warning', (warning) => {
assert.equal(warning.name, 'NewRelicWarning')
assert.ok(warning.message)
end()
})

let huge = 'a'
while (huge.length < Logger.MAX_LOG_BUFFER / 2) {
huge += huge
}

try {
logger.fatal('some message to start the buffer off')
logger.fatal(huge)
logger.fatal(huge)
} catch (error) {
assert.ifError(error)
}
})

/**
Expand Down
Loading

0 comments on commit e33807b

Please sign in to comment.