-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Updated q, superagent, and when tests to node:test (#2773)
- Loading branch information
1 parent
af3bbcd
commit ff2d8d0
Showing
14 changed files
with
1,731 additions
and
1,672 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
"q": ">=1.3.0 <2" | ||
}, | ||
"files": [ | ||
"q.tap.js" | ||
"q.test.js" | ||
] | ||
} | ||
] | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
} | ||
}, | ||
"files": [ | ||
"async-await.tap.js", | ||
"superagent.tap.js" | ||
"async-await.test.js", | ||
"superagent.test.js" | ||
] | ||
}] | ||
} |
Oops, something went wrong.