Skip to content

Commit

Permalink
test: Migrated test/smoke/api tests to node:test (#2772)
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 authored Nov 20, 2024
1 parent 440a0af commit af3bbcd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*/

'use strict'

const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const configurator = require('../../../lib/config')
const Agent = require('../../../lib/agent')
const { getTestSecret } = require('../../helpers/secrets')

const license = getTestSecret('TEST_LICENSE')
tap.test('Collector API should connect to staging-collector.newrelic.com', (t) => {
test('Collector API should connect to staging-collector.newrelic.com', (t, end) => {
const config = configurator.initialize({
app_name: 'node.js Tests',
license_key: license,
Expand All @@ -32,20 +32,20 @@ tap.test('Collector API should connect to staging-collector.newrelic.com', (t) =
const api = agent.collector

api.connect(function (error, response) {
t.error(error, 'connected without error')
assert.ok(!error, 'connected without error')

const returned = response && response.payload
t.ok(returned, 'got boot configuration')
t.ok(returned.agent_run_id, 'got run ID')
t.ok(agent.config.run_id, 'run ID set in configuration')
assert.ok(returned, 'got boot configuration')
assert.ok(returned.agent_run_id, 'got run ID')
assert.ok(agent.config.run_id, 'run ID set in configuration')

api.shutdown(function (error) {
t.error(error, 'should have shut down without issue')
t.notOk(agent.config.run_id, 'run ID should have been cleared by shutdown')
assert.ok(!error, 'should have shut down without issue')
assert.ok(!agent.config.run_id, 'run ID should have been cleared by shutdown')

agent.stop((err) => {
t.error(err, 'should not fail to stop')
t.end()
assert.ok(!err, 'should not fail to stop')
end()
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*/

'use strict'

const test = require('tap').test
const test = require('node:test')
const assert = require('node:assert')
const configurator = require('../../../lib/config')
const Agent = require('../../../lib/agent')
const { getTestSecret } = require('../../helpers/secrets')

const license = getTestSecret('TEST_LICENSE')
test('Collector API should send errors to staging-collector.newrelic.com', (t) => {
test('Collector API should send errors to staging-collector.newrelic.com', (t, end) => {
const config = configurator.initialize({
app_name: 'node.js Tests',
license_key: license,
Expand All @@ -32,26 +32,26 @@ test('Collector API should send errors to staging-collector.newrelic.com', (t) =
const api = agent.collector

api.connect(function (error) {
t.error(error, 'connected without error')
assert.ok(!error, 'connected without error')

let transaction
const proxy = agent.tracer.transactionProxy(function () {
transaction = agent.getTransaction()
transaction.finalizeNameFromUri('/nonexistent', 501)
})
proxy()
t.ok(transaction, 'got a transaction')
assert.ok(transaction, 'got a transaction')
agent.errors.add(transaction, new Error('test error'))

const payload = [agent.config.run_id, agent.errors.traceAggregator.errors]

api.send('error_data', payload, function (error, command) {
t.error(error, 'sent errors without error')
t.notOk(command.returned, 'return value is null')
assert.ok(!error, 'sent errors without error')
assert.ok(!command.returned, 'return value is null')

agent.stop((err) => {
t.error(err, 'should not fail to stop')
t.end()
assert.ok(!err, 'should not fail to stop')
end()
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
*/

'use strict'

const test = require('tap').test
const test = require('node:test')
const assert = require('node:assert')
const configurator = require('../../../lib/config')
const Agent = require('../../../lib/agent')
const CollectorAPI = require('../../../lib/collector/api')
const { getTestSecret } = require('../../helpers/secrets')

const license = getTestSecret('TEST_LICENSE')
test('Collector API should send metrics to staging-collector.newrelic.com', (t) => {
test('Collector API should send metrics to staging-collector.newrelic.com', (t, end) => {
const config = configurator.initialize({
app_name: 'node.js Tests',
license_key: license,
Expand All @@ -34,24 +34,23 @@ test('Collector API should send metrics to staging-collector.newrelic.com', (t)
const api = new CollectorAPI(agent)

api.connect(function (error) {
t.notOk(error, 'connected without error')
assert.ok(!error, 'connected without error')

agent.metrics.measureMilliseconds('TEST/discard', null, 101)

const metrics = agent.metrics._metrics

const metricJson = metrics.toJSON()
t.ok(metricJson.length >= 2, 'Should have at least two metrics.')
assert.ok(metricJson.length >= 2, 'Should have at least two metrics.')

const payload = [agent.config.run_id, metrics.started / 1000, Date.now() / 1000, metrics]

api.send('metric_data', payload, function (error, command) {
t.notOk(error, 'sent metrics without error')
t.ok(command, 'got a response')

t.same(command, { retainData: false })
assert.ok(!error, 'sent metrics without error')
assert.ok(command, 'got a response')

t.end()
assert.deepEqual(command, { retainData: false })
end()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*/

'use strict'

const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const configurator = require('../../../lib/config')
const Agent = require('../../../lib/agent')
const { getTestSecret } = require('../../helpers/secrets')

const license = getTestSecret('TEST_LICENSE')
tap.test('Collector API should send transaction traces to staging-collector.newrelic.com', (t) => {
test('Collector API should send transaction traces to staging-collector.newrelic.com', (t, end) => {
const config = configurator.initialize({
app_name: 'node.js Tests',
license_key: license,
Expand All @@ -32,7 +32,7 @@ tap.test('Collector API should send transaction traces to staging-collector.newr
const api = agent.collector

api.connect(function (error) {
t.error(error, 'connected without error')
assert.ok(!error, 'connected without error')

let transaction
const proxy = agent.tracer.transactionProxy(function () {
Expand All @@ -44,21 +44,21 @@ tap.test('Collector API should send transaction traces to staging-collector.newr
// ensure it's slow enough to get traced
transaction.trace.setDurationInMillis(5001)
transaction.end()
t.ok(agent.traces.trace, 'have a slow trace to send')
assert.ok(agent.traces.trace, 'have a slow trace to send')

agent.traces.trace.generateJSON((err, encoded) => {
t.error(err, 'should encode trace without error')
t.ok(encoded, 'have the encoded trace')
assert.ok(!err, 'should encode trace without error')
assert.ok(encoded, 'have the encoded trace')

const payload = [agent.config.run_id, [encoded]]

api.send('transaction_sample_data', payload, function (error, command) {
t.error(error, 'sent transaction trace without error')
t.notOk(command.returned, 'return value is null')
assert.ok(!error, 'sent transaction trace without error')
assert.ok(!command.returned, 'return value is null')

agent.stop((err) => {
t.error(err, 'should not fail to stop')
t.end()
assert.ok(!err, 'should not fail to stop')
end()
})
})
})
Expand Down

0 comments on commit af3bbcd

Please sign in to comment.