Skip to content

Commit

Permalink
test: updated tests to include missing coverage on some edge cases ar…
Browse files Browse the repository at this point in the history
…ound not binding segments if not in active transaction
  • Loading branch information
bizob2828 committed Apr 15, 2024
1 parent f01672e commit d2a1b10
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
13 changes: 13 additions & 0 deletions lib/instrumentation/superagent/tests/unit/superagent.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const tap = require('tap')
const utils = require('@newrelic/test-utilities')
const sinon = require('sinon')

tap.test('SuperAgent instrumentation', (t) => {
const helper = utils.TestAgent.makeInstrumented()
Expand All @@ -25,3 +26,15 @@ tap.test('SuperAgent instrumentation', (t) => {

t.end()
})

tap.test('should not wrap superagent if it is not a function', (t) => {
const mockAgent = new utils.TestAgent()
const api = mockAgent.getAgentApi()
api.shim.logger.debug = sinon.stub()
const instrumentation = require('../../lib/instrumentation')
const superagentMock = { foo: 'bar' }
instrumentation(api.shim, superagentMock)
t.equal(api.shim.logger.debug.callCount, 1, 'should call debug logger')
t.equal(api.shim.logger.debug.args[0][0], 'Not wrapping export, expected a function.')
t.end()
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ tap.test('SuperAgent instrumentation with async/await', (t) => {
let request = null
t.beforeEach(() => {
helper = utils.TestAgent.makeInstrumented()
helper.registerInstrumentation({
moduleName: 'superagent',
type: 'generic',
onRequire: require('../../lib/instrumentation'),
onError: t.bailout
})
const hooks = require('../../nr-hooks.js')
hooks.forEach(helper.registerInstrumentation)
request = require('superagent')
})
t.afterEach(() => {
Expand All @@ -45,4 +41,10 @@ tap.test('SuperAgent instrumentation with async/await', (t) => {
t.end()
})
})

t.test('should not create segment if not in a transaction', async (t) => {
await request.get('https://newrelic.com')
t.notOk(helper.getTransaction(), 'should not have a transaction')
t.end()
})
})
22 changes: 16 additions & 6 deletions lib/instrumentation/superagent/tests/versioned/superagent.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ tap.test('SuperAgent instrumentation', (t) => {
let request = null
t.beforeEach(() => {
helper = utils.TestAgent.makeInstrumented()
helper.registerInstrumentation({
moduleName: 'superagent',
type: 'generic',
onRequire: require('../../lib/instrumentation'),
onError: t.bailout
})
const hooks = require('../../nr-hooks.js')
hooks.forEach(helper.registerInstrumentation)
request = require('superagent')
})
t.afterEach(() => {
Expand All @@ -46,6 +42,13 @@ tap.test('SuperAgent instrumentation', (t) => {
})
})

t.test('should not create a segment for callback if not in transaction', (t) => {
request.get('https://newrelic.com', function testCallback() {
t.notOk(helper.getTransaction(), 'should not have a transaction')
t.end()
})
})

t.test('should maintain transaction context with promises', (t) => {
helper.runInTransaction((tx) => {
request.get('https://newrelic.com').then(function testThen() {
Expand All @@ -60,4 +63,11 @@ tap.test('SuperAgent instrumentation', (t) => {
})
})
})

t.test('should not create segment for a promise if not in a transaction', (t) => {
request.get('https://newrelic.com').then(function testThen() {
t.notOk(helper.getTransaction(), 'should not have a transaction')
t.end()
})
})
})

0 comments on commit d2a1b10

Please sign in to comment.