Skip to content

Commit

Permalink
chore: Updated q, superagent, and when tests to node:test (#2773)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr authored Nov 20, 2024
1 parent af3bbcd commit ff2d8d0
Show file tree
Hide file tree
Showing 14 changed files with 1,731 additions and 1,672 deletions.
2 changes: 1 addition & 1 deletion test/versioned/q/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"q": ">=1.3.0 <2"
},
"files": [
"q.tap.js"
"q.test.js"
]
}
]
Expand Down
130 changes: 0 additions & 130 deletions test/versioned/q/q.tap.js

This file was deleted.

113 changes: 113 additions & 0 deletions test/versioned/q/q.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

const test = require('node:test')
const assert = require('node:assert')
const tspl = require('@matteo.collina/tspl')

const { removeModules } = require('../../lib/cache-buster')
const tempRemoveListeners = require('../../lib/temp-remove-listeners')
const helper = require('../../lib/agent_helper')

function assertTransaction(agent, tx, expect = assert) {
expect.equal(agent.getTransaction(), tx)
expect.equal(agent.getTransaction().trace.root.children.length, 0)
}

test.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.agent = helper.instrumentMockedAgent()
ctx.nr.q = require('q')
})

test.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
removeModules(['q'])
})

test('q.invoke', (t, end) => {
const { agent, q } = t.nr
const firstTest = q.defer()
const secondTest = q.defer()

helper.runInTransaction(agent, (tx) => {
q.ninvoke(() => {
assertTransaction(agent, tx)
firstTest.resolve()
})
})

helper.runInTransaction(agent, (tx) => {
q.ninvoke(() => {
assertTransaction(agent, tx)
secondTest.resolve()
})
})

q.all([firstTest, secondTest]).then(() => end())
})

test('q.then', (t, end) => {
const { agent, q } = t.nr
const firstTest = q.defer()
const secondTest = q.defer()

helper.runInTransaction(agent, (tx) => {
q(true).then(function () {
assertTransaction(agent, tx)
firstTest.resolve()
})
})

helper.runInTransaction(agent, (tx) => {
q(true).then(function () {
assertTransaction(agent, tx)
secondTest.resolve()
})
})

q.all([firstTest, secondTest]).then(() => end())
})

test('q.then rejections', async (t) => {
const plan = tspl(t, { plan: 4 })
const { agent, q } = t.nr

tempRemoveListeners({ t, emitter: process, event: 'unhandledRejection' })

const firstTest = q.defer()
const secondTest = q.defer()

helper.runInTransaction(agent, (tx) => {
const thrownError = new Error('First unhandled error')
process.on('unhandledRejection', (error) => {
if (error === thrownError) {
assertTransaction(agent, tx, plan)
firstTest.resolve()
}
})
q(true).then(() => {
throw thrownError
})
})

helper.runInTransaction(agent, (tx) => {
const thrownError = new Error('Second unhandled error')
process.on('unhandledRejection', (error) => {
if (error === thrownError) {
assertTransaction(agent, tx, plan)
secondTest.resolve()
}
})
q(true).then(() => {
throw thrownError
})
})

q.all([firstTest.promise, secondTest.promise])
await plan.completed
})
60 changes: 0 additions & 60 deletions test/versioned/superagent/async-await.tap.js

This file was deleted.

61 changes: 61 additions & 0 deletions test/versioned/superagent/async-await.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

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

const { removeModules } = require('../../lib/cache-buster')
const match = require('../../lib/custom-assertions/match')
const helper = require('../../lib/agent_helper')
const testServer = require('./test-server')

const EXTERNAL_NAME = /External\/127.0.0.1:\d+\//

test.beforeEach(async (ctx) => {
ctx.nr = {}
ctx.nr.agent = helper.instrumentMockedAgent()

const { address, server, stopServer } = await testServer()
ctx.nr.address = address
ctx.nr.server = server
ctx.nr.stopServer = stopServer

ctx.nr.request = require('superagent')
})

test.afterEach(async (ctx) => {
helper.unloadAgent(ctx.nr.agent)
removeModules(['superagent'])
await ctx.nr.stopServer()
})

test('should maintain transaction context with promises', (t, end) => {
const { address, agent } = t.nr
helper.runInTransaction(agent, async function (tx) {
assert.ok(tx)

const { request } = t.nr
await request.get(address)

const mainSegment = tx.trace.root.children[0]
assert.ok(mainSegment)
match(mainSegment.name, EXTERNAL_NAME, 'has segment matching request')
assert.equal(
mainSegment.children.filter((c) => c.name === 'Callback: <anonymous>').length,
1,
'CB created by superagent is present'
)

end()
})
})

test('should not create segment if not in a transaction', async (t) => {
const { address, agent, request } = t.nr
await request.get(address)
assert.equal(agent.getTransaction(), undefined, 'should not have a transaction')
})
4 changes: 2 additions & 2 deletions test/versioned/superagent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
}
},
"files": [
"async-await.tap.js",
"superagent.tap.js"
"async-await.test.js",
"superagent.test.js"
]
}]
}
Loading

0 comments on commit ff2d8d0

Please sign in to comment.