diff --git a/test/unit/serverless/api-gateway-v2.test.js b/test/unit/serverless/api-gateway-v2.test.js index e07a6562ba..d171b91759 100644 --- a/test/unit/serverless/api-gateway-v2.test.js +++ b/test/unit/serverless/api-gateway-v2.test.js @@ -64,7 +64,7 @@ test('should pick up the arn', async (t) => { assert.equal(agent.collector.metadata.arn, functionContext.invokedFunctionArn) }) -test('should create a web transaction', (t, end) => { +test('should create a web transaction', async (t) => { const plan = tspl(t, { plan: 8 }) const { agent, lambda, event, functionContext, responseBody } = t.nr agent.on('transactionFinished', verifyAttributes) @@ -90,21 +90,16 @@ test('should create a web transaction', (t, end) => { plan.equal(agentAttributes['request.uri'], '/my/path') plan.equal(spanAttributes['request.method'], 'POST') plan.equal(spanAttributes['request.uri'], '/my/path') - - end() } + await plan.completed }) -test('should set w3c tracecontext on transaction if present on request header', (t, end) => { +test('should set w3c tracecontext on transaction if present on request header', async (t) => { const plan = tspl(t, { plan: 2 }) const expectedTraceId = '4bf92f3577b34da6a3ce929d0e0e4736' const traceparent = `00-${expectedTraceId}-00f067aa0ba902b7-00` const { agent, lambda, event, functionContext, responseBody } = t.nr - agent.on('transactionFinished', () => { - end() - }) - agent.config.distributed_tracing.enabled = true event.headers.traceparent = traceparent @@ -124,15 +119,13 @@ test('should set w3c tracecontext on transaction if present on request header', }) wrappedHandler(event, functionContext, () => {}) + await plan.completed }) -test('should add w3c tracecontext to transaction if not present on request header', (t, end) => { +test('should add w3c tracecontext to transaction if not present on request header', async (t) => { const plan = tspl(t, { plan: 2 }) const { agent, lambda, event, functionContext, responseBody } = t.nr - agent.on('transactionFinished', () => { - end() - }) agent.config.account_id = 'AccountId1' agent.config.primary_application_id = 'AppId1' @@ -152,9 +145,10 @@ test('should add w3c tracecontext to transaction if not present on request heade }) wrappedHandler(event, functionContext, () => {}) + await plan.completed }) -test('should capture request parameters', (t, end) => { +test('should capture request parameters', async (t) => { const plan = tspl(t, { plan: 5 }) const { agent, lambda, event, functionContext, responseBody } = t.nr @@ -182,12 +176,11 @@ test('should capture request parameters', (t, end) => { plan.equal(spanAttributes['request.parameters.team'], 'node agent') plan.equal(agentAttributes['request.parameters.parameter1'], 'value1,value2') - - end() } + await plan.completed }) -test('should capture request headers', (t, end) => { +test('should capture request headers', async (t) => { const plan = tspl(t, { plan: 2 }) const { agent, lambda, event, functionContext, responseBody } = t.nr @@ -203,9 +196,8 @@ test('should capture request headers', (t, end) => { plan.equal(attrs['request.headers.accept'], 'application/json') plan.equal(attrs['request.headers.header2'], 'value1,value2') - - end() } + await plan.completed }) test('should not crash when headers are non-existent', (t) => { diff --git a/test/unit/serverless/aws-lambda.test.js b/test/unit/serverless/aws-lambda.test.js index 00e529601d..2fd3772379 100644 --- a/test/unit/serverless/aws-lambda.test.js +++ b/test/unit/serverless/aws-lambda.test.js @@ -1397,7 +1397,7 @@ test('AwsLambda.patchLambdaHandler', async (t) => { } ) - await t.test('should record error event when error is thrown', (t, end) => { + await t.test('should record error event when error is thrown', async (t) => { const plan = tspl(t, { plan: 8 }) const { agent, awsLambda, error, stubEvent, stubContext, stubCallback } = t.nr @@ -1427,8 +1427,8 @@ test('AwsLambda.patchLambdaHandler', async (t) => { if (error.name !== 'SyntaxError') { throw error } - end() } + await plan.completed }) await t.test('should not end transactions twice', (t, end) => { diff --git a/test/unit/shim/message-shim.test.js b/test/unit/shim/message-shim.test.js index 30e578c960..00ad948b9e 100644 --- a/test/unit/shim/message-shim.test.js +++ b/test/unit/shim/message-shim.test.js @@ -370,7 +370,7 @@ test('MessageShim', async function (t) { }) }) - await t.test('should insert distributed trace headers in all messages', function (t, end) { + await t.test('should insert distributed trace headers in all messages', async function (t) { const plan = tspl(t, { plan: 1 }) const { agent, shim, wrappable } = t.nr const messages = [{}, { headers: { foo: 'foo' } }, {}] @@ -418,13 +418,13 @@ test('MessageShim', async function (t) { } ]) plan.equal(called, 1) - end() }) helper.runInTransaction(agent, (tx) => { wrappable.sendMessages() tx.end() }) + await plan.completed }) await t.test('should create message broker metrics', function (t, end) { @@ -1241,7 +1241,7 @@ test('MessageShim', async function (t) { }) }) - await t.test('should wrap object key of consumer', function (t, end) { + await t.test('should wrap object key of consumer', async function (t) { const plan = tspl(t, { plan: 4 }) const { shim } = t.nr const message = { foo: 'bar' } @@ -1267,10 +1267,10 @@ test('MessageShim', async function (t) { const segment = shim.getSegment() plan.equal(segment.name, 'OtherTransaction/Message/RabbitMQ/Exchange/Named/exchange.foo') plan.equal(msg, message) - end() } } wrapped(handler) + await plan.completed }) }) }) diff --git a/test/unit/shim/shim.test.js b/test/unit/shim/shim.test.js index 0e50498b9f..40191d5263 100644 --- a/test/unit/shim/shim.test.js +++ b/test/unit/shim/shim.test.js @@ -1184,7 +1184,7 @@ test('Shim', async function (t) { t.afterEach(afterEach) - await t.test('should make the segment translucent when promise resolves', function (t, end) { + await t.test('should make the segment translucent when promise resolves', async function (t) { const plan = tspl(t, { plan: 4 }) const { agent, promise, resolve, shim, toWrap } = t.nr const wrapped = shim.record(toWrap, function () { @@ -1196,22 +1196,20 @@ test('Shim', async function (t) { const ret = wrapped() plan.ok(ret instanceof Object.getPrototypeOf(promise).constructor) - ret - .then(function (val) { - plan.equal(result, val) - plan.equal(promise.segment.opaque, false) - end() - }) - .catch(end) + ret.then(function (val) { + plan.equal(result, val) + plan.equal(promise.segment.opaque, false) + }) }) plan.equal(promise.segment.opaque, true) setTimeout(function () { resolve(result) }, 5) + await plan.completed }) - await t.test('should touch the segment when promise resolves', function (t, end) { + await t.test('should touch the segment when promise resolves', async function (t) { const plan = tspl(t, { plan: 3 }) const { agent, promise, resolve, shim, toWrap } = t.nr const wrapped = shim.record(toWrap, function () { @@ -1224,21 +1222,19 @@ test('Shim', async function (t) { const oldDur = promise.segment.timer.getDurationInMillis() plan.ok(ret instanceof Object.getPrototypeOf(promise).constructor) - ret - .then(function (val) { - plan.equal(result, val) - plan.ok(promise.segment.timer.getDurationInMillis() > oldDur) - end() - }) - .catch(end) + ret.then(function (val) { + plan.equal(result, val) + plan.ok(promise.segment.timer.getDurationInMillis() > oldDur) + }) }) setTimeout(function () { resolve(result) }, 5) + await plan.completed }) - await t.test('should make the segment translucent when promise rejects', function (t, end) { + await t.test('should make the segment translucent when promise rejects', async function (t) { const plan = tspl(t, { plan: 4 }) const { agent, promise, reject, shim, toWrap } = t.nr const wrapped = shim.record(toWrap, function () { @@ -1250,27 +1246,25 @@ test('Shim', async function (t) { const ret = wrapped() plan.ok(ret instanceof Object.getPrototypeOf(promise).constructor) - ret - .then( - function () { - end(new Error('Should not have resolved!')) - }, - function (err) { - plan.equal(err, result) - plan.equal(promise.segment.opaque, false) - end() - } - ) - .catch(end) + ret.then( + function () { + throw new Error('Should not have resolved!') + }, + function (err) { + plan.equal(err, result) + plan.equal(promise.segment.opaque, false) + } + ) }) plan.equal(promise.segment.opaque, true) setTimeout(function () { reject(result) }, 5) + await plan.completed }) - await t.test('should touch the segment when promise rejects', function (t, end) { + await t.test('should touch the segment when promise rejects', async function (t) { const plan = tspl(t, { plan: 3 }) const { agent, promise, reject, shim, toWrap } = t.nr const wrapped = shim.record(toWrap, function () { @@ -1288,7 +1282,6 @@ test('Shim', async function (t) { function (err) { plan.equal(err, result) plan.ok(promise.segment.timer.getDurationInMillis() > oldDur) - end() } ) }) @@ -1296,12 +1289,12 @@ test('Shim', async function (t) { setTimeout(function () { reject(result) }, 5) + await plan.completed }) await t.test('should not affect unhandledRejection event', async (t) => { const plan = tspl(t, { plan: 2 }) const { agent, promise, reject, shim, toWrap } = t.nr - const { promise: testPromise, resolve: testResolve } = promiseResolvers() const result = new Error('unhandled rejection test') tempOverrideUncaught({ @@ -1309,7 +1302,6 @@ test('Shim', async function (t) { type: tempOverrideUncaught.REJECTION, handler(err) { plan.deepEqual(err, result) - testResolve() } }) @@ -1328,13 +1320,12 @@ test('Shim', async function (t) { reject(result) }, 5) - await testPromise + await plan.completed }) await t.test('should call after hook when promise resolves', async (t) => { const plan = tspl(t, { plan: 7 }) const { agent, promise, resolve, shim, toWrap } = t.nr - const { promise: testPromise, resolve: testResolve } = promiseResolvers() const segmentName = 'test segment' const expectedResult = { returned: true } const wrapped = shim.record(toWrap, function () { @@ -1349,7 +1340,6 @@ test('Shim', async function (t) { plan.equal(error, null) plan.deepEqual(result, expectedResult) plan.equal(segment.name, segmentName) - testResolve() } }) }) @@ -1363,13 +1353,12 @@ test('Shim', async function (t) { resolve(expectedResult) }, 5) - await testPromise + await plan.completed }) await t.test('should call after hook when promise reject', async (t) => { const plan = tspl(t, { plan: 6 }) const { agent, promise, reject, shim, toWrap } = t.nr - const { promise: testPromise, resolve: testResolve } = promiseResolvers() const segmentName = 'test segment' const expectedResult = new Error('should call after hook when promise rejects') const wrapped = shim.record(toWrap, function () { @@ -1383,7 +1372,6 @@ test('Shim', async function (t) { plan.equal(name, toWrap.name) plan.deepEqual(error, expectedResult) plan.equal(segment.name, segmentName) - testResolve() } }) }) @@ -1396,7 +1384,7 @@ test('Shim', async function (t) { setTimeout(function () { reject(expectedResult) }, 5) - await testPromise + await plan.completed }) }) diff --git a/test/versioned/express/app-use.test.js b/test/versioned/express/app-use.test.js index 1c3d127167..9eff58cd48 100644 --- a/test/versioned/express/app-use.test.js +++ b/test/versioned/express/app-use.test.js @@ -9,11 +9,10 @@ const helper = require('../../lib/agent_helper') const http = require('http') const { isExpress5 } = require('./utils') const tsplan = require('@matteo.collina/tspl') -const promiseResolvers = require('../../lib/promise-resolvers') // This test is no longer applicable in express 5 as mounting a child router does not emit the same // mount event -test('app should be at top of stack when mounted', { skip: isExpress5() }, function (t, end) { +test('app should be at top of stack when mounted', { skip: isExpress5() }, async function (t) { const agent = helper.instrumentMockedAgent() const express = require('express') @@ -28,14 +27,13 @@ test('app should be at top of stack when mounted', { skip: isExpress5() }, funct child.on('mount', function () { plan.equal(main._router.stack.length, 3, '3 middleware functions: query parser, Express, child') - end() }) main.use(child) + await plan.completed }) test('app should be at top of stack when mounted', async function (t) { - const { promise, resolve } = promiseResolvers() const agent = helper.instrumentMockedAgent() const express = require('express') @@ -45,19 +43,10 @@ test('app should be at top of stack when mounted', async function (t) { const router = new express.Router() const router2 = new express.Router() const server = http.createServer(main) - // track how many requests and wait for all to be done - let tests = 0 - - const int = setInterval(() => { - if (tests === 5) { - resolve() - } - }, 10) t.after(function () { helper.unloadAgent(agent) server.close() - clearInterval(int) }) main.use('/:app', app) @@ -88,7 +77,6 @@ test('app should be at top of stack when mounted', async function (t) { 'Expressjs/GET//:app/:child/app', 'should set partialName correctly for nested apps' ) - ++tests }) helper.makeGetRequest(host + '/myApp/nestedApp ', function (err, res, body) { @@ -98,7 +86,6 @@ test('app should be at top of stack when mounted', async function (t) { 'Expressjs/GET//:app/nestedApp', 'should set partialName correctly for deeply nested apps' ) - ++tests }) helper.makeGetRequest(host + '/myApp/myChild/router', function (err, res, body) { @@ -108,7 +95,6 @@ test('app should be at top of stack when mounted', async function (t) { 'Expressjs/GET//:router/:child/router', 'should set partialName correctly for nested routers' ) - ++tests }) helper.makeGetRequest(host + '/myApp/nestedRouter', function (err, res, body) { @@ -118,7 +104,6 @@ test('app should be at top of stack when mounted', async function (t) { 'Expressjs/GET//:router/nestedRouter', 'should set partialName correctly for deeply nested routers' ) - ++tests }) helper.makeGetRequest(host + '/foo/bar', function (err, res, body) { @@ -128,7 +113,6 @@ test('app should be at top of stack when mounted', async function (t) { 'Expressjs/GET//:foo/:bar', 'should reset partialName after a router without a matching route' ) - ++tests }) }) }) @@ -137,11 +121,10 @@ test('app should be at top of stack when mounted', async function (t) { const tx = agent.getTransaction() res.send(tx.id) } - - await promise + await plan.completed }) -test('should not pass wrong args when transaction is not present', function (t, end) { +test('should not pass wrong args when transaction is not present', async function (t) { const plan = tsplan(t, { plan: 5 }) const agent = helper.instrumentMockedAgent() @@ -179,8 +162,8 @@ test('should not pass wrong args when transaction is not present', function (t, helper.makeGetRequest('http://localhost:' + port + '/', function (err, res, body) { plan.ok(!err) plan.equal(body, 'ok') - end() }) }) }) + await plan.completed }) diff --git a/test/versioned/express/bare-router.test.js b/test/versioned/express/bare-router.test.js index 2b23d78a79..6620487144 100644 --- a/test/versioned/express/bare-router.test.js +++ b/test/versioned/express/bare-router.test.js @@ -16,7 +16,7 @@ test.beforeEach(async (ctx) => { test.afterEach(teardown) -test('Express router introspection', function (t, end) { +test('Express router introspection', async function (t) { const { agent, app, port } = t.nr const plan = tsplan(t, { plan: 11 }) @@ -53,6 +53,6 @@ test('Express router introspection', function (t, end) { helper.makeGetRequest(url, { json: true }, function (error, res, body) { plan.equal(res.statusCode, 200, 'nothing exploded') plan.deepEqual(body, { status: 'ok' }, 'got expected response') - end() }) + await plan.completed }) diff --git a/test/versioned/express/errors.test.js b/test/versioned/express/errors.test.js index a7a4aeb236..7fbf08abc0 100644 --- a/test/versioned/express/errors.test.js +++ b/test/versioned/express/errors.test.js @@ -185,7 +185,7 @@ test('Error handling tests', async (t) => { }) }) - await t.test('does not error when request is aborted', function (t, end) { + await t.test('does not error when request is aborted', async function (t) { const plan = tsplan(t, { plan: 4 }) const { app, agent, port } = t.nr let request = null @@ -205,7 +205,6 @@ test('Error handling tests', async (t) => { app.use(function (error, req, res, next) { plan.equal(agent.getTransaction(), null, 'no active transaction when responding') res.end() - end() }) request = http.request( @@ -222,6 +221,7 @@ test('Error handling tests', async (t) => { request.on('error', function (err) { plan.equal(err.code, 'ECONNRESET') }) + await plan.completed }) }) diff --git a/test/versioned/express/express-enrouten.test.js b/test/versioned/express/express-enrouten.test.js index 495695a514..40beac47f1 100644 --- a/test/versioned/express/express-enrouten.test.js +++ b/test/versioned/express/express-enrouten.test.js @@ -20,7 +20,7 @@ test.beforeEach(async (ctx) => { test.afterEach(teardown) -test('Express + express-enrouten compatibility test', { skip: isExpress5() }, function (t, end) { +test('Express + express-enrouten compatibility test', { skip: isExpress5() }, async function (t) { const { app, port } = t.nr const plan = tsplan(t, { plan: 2 }) @@ -35,6 +35,6 @@ test('Express + express-enrouten compatibility test', { skip: isExpress5() }, fu helper.makeGetRequest('http://localhost:' + port + '/foo', function (error, res) { plan.equal(res.statusCode, 200, 'Second Route loaded') - end() }) + await plan.completed }) diff --git a/test/versioned/express/ignoring.test.js b/test/versioned/express/ignoring.test.js index e289783d32..72f6f756a7 100644 --- a/test/versioned/express/ignoring.test.js +++ b/test/versioned/express/ignoring.test.js @@ -17,7 +17,7 @@ test.beforeEach(async (ctx) => { test.afterEach(teardown) -test('ignoring an Express route', function (t, end) { +test('ignoring an Express route', async function (t) { const { agent, app, port } = t.nr const plan = tsplan(t, { plan: 7 }) @@ -57,6 +57,6 @@ test('ignoring an Express route', function (t, end) { helper.makeGetRequest(url, function (error, res, body) { plan.equal(res.statusCode, 400, 'got expected error') plan.deepEqual(body, { status: 'pollpollpoll' }, 'got expected response') - end() }) + await plan.completed }) diff --git a/test/versioned/express/issue171.test.js b/test/versioned/express/issue171.test.js index f1671270e2..6169120abc 100644 --- a/test/versioned/express/issue171.test.js +++ b/test/versioned/express/issue171.test.js @@ -16,7 +16,7 @@ test.beforeEach(async (ctx) => { test.afterEach(teardown) -test("adding 'handle' middleware", function (t, end) { +test("adding 'handle' middleware", async function (t) { const { app, port } = t.nr const plan = tsplan(t, { plan: 2 }) @@ -40,7 +40,7 @@ test("adding 'handle' middleware", function (t, end) { res.pipe(process.stderr) plan.equal(res.statusCode, 500) - end() }) .end() + await plan.completed }) diff --git a/test/versioned/express/render.test.js b/test/versioned/express/render.test.js index 56443ffc56..49648e6db3 100644 --- a/test/versioned/express/render.test.js +++ b/test/versioned/express/render.test.js @@ -45,7 +45,7 @@ test('using only the express router', function (t, end) { end() }) -test('the express router should go through a whole request lifecycle', function (t, end) { +test('the express router should go through a whole request lifecycle', async function (t) { const agent = helper.instrumentMockedAgent() const router = require('express').Router() // eslint-disable-line new-cap const finalhandler = require('finalhandler') @@ -70,9 +70,9 @@ test('the express router should go through a whole request lifecycle', function server.close() plan.ok(!error) - end() }) }) + await plan.completed }) test('agent instrumentation of Express', async function (t) { @@ -167,7 +167,8 @@ test('agent instrumentation of Express', async function (t) { } ) - await t.test('using EJS templates', { timeout: 1000 }, function (t, end) { + await t.test('using EJS templates', { timeout: 1000 }, async function (t) { + const plan = tsplan(t, { plan: 4 }) const { app, agent, port } = t.nr app.set('views', __dirname + '/views') app.set('view engine', 'ejs') @@ -178,20 +179,20 @@ test('agent instrumentation of Express', async function (t) { agent.once('transactionFinished', function () { const stats = agent.metrics.getMetric('View/index/Rendering') - assert.equal(stats.callCount, 1, 'should note the view rendering') + plan.equal(stats.callCount, 1, 'should note the view rendering') }) helper.makeGetRequest(`${TEST_URL}:${port}${TEST_PATH}`, function (error, response, body) { - assert.ok(!error, 'should not error making request') + plan.ok(!error, 'should not error making request') - assert.equal(response.statusCode, 200, 'response code should be 200') - assert.equal(body, BODY, 'template should still render fine') - - end() + plan.equal(response.statusCode, 200, 'response code should be 200') + plan.equal(body, BODY, 'template should still render fine') }) + await plan.completed }) - await t.test('should generate rum headers', { timeout: 1000 }, function (t, end) { + await t.test('should generate rum headers', { timeout: 1000 }, async function (t) { + const plan = tsplan(t, { plan: 5 }) const { app, agent, port } = t.nr const api = new API(agent) @@ -205,23 +206,22 @@ test('agent instrumentation of Express', async function (t) { app.get(TEST_PATH, function (req, res) { const rum = api.getBrowserTimingHeader() - assert.equal(rum.substring(0, 7), ' { test.afterEach(teardown) -test('Express router introspection', function (t, end) { +test('Express router introspection', async function (t) { const { agent, app, express, port } = t.nr const plan = tsplan(t, { plan: 14 }) @@ -64,6 +64,6 @@ test('Express router introspection', function (t, end) { plan.ok(!error, 'should not have errored') plan.equal(res.statusCode, 200, 'should have ok status') plan.deepEqual(body, { status: 'ok' }, 'should have expected response') - end() }) + await plan.completed }) diff --git a/test/versioned/express/transaction-naming.test.js b/test/versioned/express/transaction-naming.test.js index bc2a284cf8..eca3baf371 100644 --- a/test/versioned/express/transaction-naming.test.js +++ b/test/versioned/express/transaction-naming.test.js @@ -327,7 +327,7 @@ test('transactions running in parallel should be recorded correctly', function ( } }) -test('names transaction when request is aborted', function (t, end) { +test('names transaction when request is aborted', async function (t) { const plan = tsplan(t, { plan: 5 }) const { agent, app, port } = t.nr @@ -349,7 +349,6 @@ test('names transaction when request is aborted', function (t, end) { app.use(function (error, req, res, next) { plan.ok(agent.getTransaction() == null, 'no active transaction when responding') res.end() - end() }) request = http.request( @@ -370,9 +369,10 @@ test('names transaction when request is aborted', function (t, end) { agent.on('transactionFinished', function (tx) { plan.equal(tx.name, 'WebTransaction/Expressjs/GET//test') }) + await plan.completed }) -test('Express transaction names are unaffected by errorware', function (t, end) { +test('Express transaction names are unaffected by errorware', async function (t) { const plan = tsplan(t, { plan: 1 }) const { agent, app, port } = t.nr @@ -380,7 +380,6 @@ test('Express transaction names are unaffected by errorware', function (t, end) agent.on('transactionFinished', function (tx) { const expected = 'WebTransaction/Expressjs/GET//test' plan.equal(tx.trace.root.children[0].name, expected) - end() }) app.use('/test', function () { @@ -393,9 +392,10 @@ test('Express transaction names are unaffected by errorware', function (t, end) }) http.request({ port, path: '/test' }).end() + await plan.completed }) -test('when next is called after transaction state loss', function (t, end) { +test('when next is called after transaction state loss', async function (t) { // Uninstrumented work queue. This must be set up before the agent is loaded // so that no transaction state is maintained. const tasks = [] @@ -446,7 +446,6 @@ test('when next is called after transaction state loss', function (t, end) { res.resume() res.on('end', function () { plan.equal(transactionsFinished, 2, 'should have two transactions done') - end() }) }) @@ -457,14 +456,15 @@ test('when next is called after transaction state loss', function (t, end) { res.resume() }) }, 100) + await plan.completed }) // express did not add array based middleware registration // without path until 4.9.2 // https://github.com/expressjs/express/blob/master/History.md#492--2014-09-17 if (semver.satisfies(pkgVersion, '>=4.9.2')) { - test('transaction name with array of middleware with unspecified mount path', (t, end) => { - const plan = tsplan(t, { plan: 2 }) + test('transaction name with array of middleware with unspecified mount path', async (t) => { + const plan = tsplan(t, { plan: 3 }) const { app } = t.nr function mid1(req, res, next) { @@ -483,11 +483,12 @@ if (semver.satisfies(pkgVersion, '>=4.9.2')) { res.end() }) - runTest({ t, end, endpoint: '/path1' }) + runTest({ t, localAssert: plan, endpoint: '/path1' }) + await plan.completed }) - test('transaction name when ending in array of unmounted middleware', (t, end) => { - const plan = tsplan(t, { plan: 2 }) + test('transaction name when ending in array of unmounted middleware', async (t) => { + const plan = tsplan(t, { plan: 3 }) const { app } = t.nr function mid1(req, res, next) { @@ -504,7 +505,8 @@ if (semver.satisfies(pkgVersion, '>=4.9.2')) { app.use(mid1) - runTest({ t, end, endpoint: '/path1', expectedName: '/' }) + runTest({ t, localAssert: plan, endpoint: '/path1', expectedName: '/' }) + await plan.completed }) } @@ -534,18 +536,32 @@ function makeMultiRunner({ t, endpoint, expectedName, numTests, end }) { } } -function runTest({ t, endpoint, expectedName, end }) { +/** + * Makes a request and waits for transaction to finish before ending test. + * You can pass in the assertion library, this is for tests that rely on `tspl` + * end is optionally called and will be ommitted when tests rely on `tspl` + * to end. + * + * @param {object} params to function + * @param {object} params.t test context + * @param {string} params.endpoint + * @param {string} [params.expectedName] defaults to endpoint if not specified + * @param {function} [params.end] function that tells test to end + * @param {object} params.localAssert library for assertions, defaults to `node:assert` + * + */ +function runTest({ t, endpoint, expectedName, end, localAssert = require('node:assert') }) { const { agent, port } = t.nr if (!expectedName) { expectedName = endpoint } agent.on('transactionFinished', function (transaction) { - assert.equal( + localAssert.equal( transaction.name, 'WebTransaction/Expressjs/GET/' + expectedName, 'transaction has expected name' ) - end() + end?.() }) makeRequest(port, endpoint) }