diff --git a/test/lib/agent_helper.js b/test/lib/agent_helper.js index 7cbac195fd..7f73a1201f 100644 --- a/test/lib/agent_helper.js +++ b/test/lib/agent_helper.js @@ -17,6 +17,7 @@ const { EventEmitter } = require('events') const Transaction = require('../../lib/transaction') const symbols = require('../../lib/symbols') const InstrumentationTracker = require('../../lib/instrumentation-tracker') +const { removeModules } = require('./cache-buster') const http = require('http') const https = require('https') const semver = require('semver') @@ -220,11 +221,7 @@ helper.maybeLoadSecurityAgent = function maybeLoadSecurityAgent(agent) { */ helper.maybeUnloadSecurityAgent = function maybeUnloadSecurityAgent(agent) { if (helper.isSecurityAgentEnabled(agent)) { - Object.keys(require.cache).forEach((key) => { - if (key.includes('@newrelic/security-agent')) { - delete require.cache[key] - } - }) + removeModules(['@newrelic/security-agent']) } } diff --git a/test/lib/cache-buster.js b/test/lib/cache-buster.js new file mode 100644 index 0000000000..93cbcccde3 --- /dev/null +++ b/test/lib/cache-buster.js @@ -0,0 +1,55 @@ +/* + * Copyright 2024 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +/** + * Utility method to remove a set of modules from the require cache. + * + * @param {string[]} modules The set of module names to remove from the cache. + */ +module.exports = { + /** + * Removes explicitly named modules from the require cache. + * + * @param {string[]} modules + * + * @returns {number} The number of cache entries removed. + */ + removeModules(modules = []) { + let removed = 0 + const keys = Object.keys(require.cache) + for (const mod of modules) { + for (const key of keys) { + if (key.includes(mod) === false) { + continue + } + delete require.cache[key] + removed += 1 + } + } + return removed + }, + + /** + * Removes modules from the require cache that are identified by a matcher. + * + * @param {RegExp} matcher + * + * @returns {number} The number of cache entries removed. + */ + removeMatchedModules(matcher) { + let removed = 0 + const keys = Object.keys(require.cache) + for (const key of keys) { + if (matcher.test(key) === false) { + continue + } + delete require.cache[key] + removed += 1 + } + return removed + } +} diff --git a/test/unit/config/config-location.test.js b/test/unit/config/config-location.test.js index 3ed4c1bbe9..ae0ce87da8 100644 --- a/test/unit/config/config-location.test.js +++ b/test/unit/config/config-location.test.js @@ -11,6 +11,7 @@ const fs = require('fs') const fsPromises = require('fs/promises') const sinon = require('sinon') +const { removeMatchedModules } = require('../../lib/cache-buster') const Config = require('../../../lib/config') tap.test('when overriding the config file location via NEW_RELIC_HOME', (t) => { @@ -153,11 +154,7 @@ tap.test('Selecting config file path', (t) => { process.chdir(originalWorkingDirectory) const mainModuleRegex = new RegExp(MAIN_MODULE_DIR) - Object.keys(require.cache).forEach((key) => { - if (mainModuleRegex.test(key)) { - delete require.cache[key] - } - }) + removeMatchedModules(mainModuleRegex) }) t.test('should load the default newrelic.js config file', (t) => { diff --git a/test/unit/instrumentation/koa/koa.test.js b/test/unit/instrumentation/koa/koa.test.js index 47bb083be1..f9c84c8729 100644 --- a/test/unit/instrumentation/koa/koa.test.js +++ b/test/unit/instrumentation/koa/koa.test.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../../lib/agent_helper') +const { removeModules } = require('../../../lib/cache-buster') const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor') tap.beforeEach((t) => { @@ -23,11 +24,7 @@ tap.beforeEach((t) => { tap.afterEach((t) => { helper.unloadAgent(t.context.agent) - Object.keys(require.cache).forEach((key) => { - if (key.includes('koa')) { - delete require.cache[key] - } - }) + removeModules(['koa']) }) tap.test('Koa instrumentation', async (t) => { diff --git a/test/unit/instrumentation/koa/route.test.js b/test/unit/instrumentation/koa/route.test.js index 25f47d5d06..38bf850c95 100644 --- a/test/unit/instrumentation/koa/route.test.js +++ b/test/unit/instrumentation/koa/route.test.js @@ -8,6 +8,7 @@ const tap = require('tap') const { METHODS } = require('../../../../lib/instrumentation/http-methods') const helper = require('../../../lib/agent_helper') +const { removeModules } = require('../../../lib/cache-buster') const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor') tap.beforeEach((t) => { @@ -24,11 +25,7 @@ tap.beforeEach((t) => { tap.afterEach((t) => { helper.unloadAgent(t.context.agent) - Object.keys(require.cache).forEach((key) => { - if (key.includes('koa-route')) { - delete require.cache[key] - } - }) + removeModules(['koa-route']) }) tap.test('methods', function (t) { diff --git a/test/unit/instrumentation/koa/router.test.js b/test/unit/instrumentation/koa/router.test.js index dd34269ae0..300bf85a14 100644 --- a/test/unit/instrumentation/koa/router.test.js +++ b/test/unit/instrumentation/koa/router.test.js @@ -10,6 +10,7 @@ const tap = require('tap') const instrumentation = require('../../../../lib/instrumentation/koa/router-instrumentation') const { METHODS } = require('../../../../lib/instrumentation/http-methods') const helper = require('../../../lib/agent_helper') +const { removeModules } = require('../../../lib/cache-buster') const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor') const WRAPPED_METHODS = ['param', 'register', 'routes', 'middleware', 'allowedMethods'] const UNWRAPPED_METHODS = METHODS.concat([ @@ -45,11 +46,7 @@ tap.test('koa-router', (t) => { t.afterEach((t) => { helper.unloadAgent(t.context.agent) - Object.keys(require.cache).forEach((key) => { - if (key.includes(koaRouterMod)) { - delete require.cache[key] - } - }) + removeModules([koaRouterMod]) }) t.test('mounting paramware', async (t) => { @@ -99,11 +96,7 @@ tap.test('koa-router', (t) => { t.afterEach((t) => { helper.unloadAgent(t.context.agent) - Object.keys(require.cache).forEach((key) => { - if (key.includes(koaRouterMod)) { - delete require.cache[key] - } - }) + removeModules([koaRouterMod]) }) t.test('mounting paramware', async (t) => { diff --git a/test/versioned/amqplib/callback.tap.js b/test/versioned/amqplib/callback.tap.js index 3bb73a5afe..8b5cc91c7e 100644 --- a/test/versioned/amqplib/callback.tap.js +++ b/test/versioned/amqplib/callback.tap.js @@ -8,6 +8,7 @@ const amqpUtils = require('./amqp-utils') const API = require('../../../api') const helper = require('../../lib/agent_helper') +const { removeMatchedModules } = require('../../lib/cache-buster') const tap = require('tap') /* @@ -64,11 +65,7 @@ tap.test('amqplib callback instrumentation', function (t) { t.afterEach(function () { helper.unloadAgent(agent) - Object.keys(require.cache).forEach(function (key) { - if (/amqplib/.test(key)) { - delete require.cache[key] - } - }) + removeMatchedModules(/amqplib/) if (!conn) { return diff --git a/test/versioned/amqplib/promises.tap.js b/test/versioned/amqplib/promises.tap.js index 5d3e062f29..73ceea75d1 100644 --- a/test/versioned/amqplib/promises.tap.js +++ b/test/versioned/amqplib/promises.tap.js @@ -8,6 +8,7 @@ const amqpUtils = require('./amqp-utils') const API = require('../../../api') const helper = require('../../lib/agent_helper') +const { removeMatchedModules } = require('../../lib/cache-buster') const tap = require('tap') /* @@ -37,11 +38,7 @@ tap.test('amqplib promise instrumentation', function (t) { // which the test itself re-requires, but second-order modules (deps of // instrumented methods) are not reloaded and thus not re-instrumented. To // resolve this we just delete everything. Kill it all. - Object.keys(require.cache).forEach(function (key) { - if (/amqplib|bluebird/.test(key)) { - delete require.cache[key] - } - }) + removeMatchedModules(/amqplib|bluebird/) agent = helper.instrumentMockedAgent({ attributes: { diff --git a/test/versioned/bunyan/bunyan.tap.js b/test/versioned/bunyan/bunyan.tap.js index 4c37f33106..e54dc608bb 100644 --- a/test/versioned/bunyan/bunyan.tap.js +++ b/test/versioned/bunyan/bunyan.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeMatchedModules } = require('../../lib/cache-buster') require('../../lib/logging-helper') const { LOGGING } = require('../../../lib/metrics/names') const { makeSink, logStuff, originalMsgAssertion, logForwardingMsgAssertion } = require('./helpers') @@ -28,11 +29,7 @@ tap.test('bunyan instrumentation', (t) => { bunyan = null // must purge require cache of bunyan related instrumentation // to ensure it re-registers on subsequent test runs - Object.keys(require.cache).forEach((key) => { - if (/bunyan/.test(key)) { - delete require.cache[key] - } - }) + removeMatchedModules(/bunyan/) }) t.test('logging disabled', (t) => { diff --git a/test/versioned/grpc/client-bidi-streaming.tap.js b/test/versioned/grpc/client-bidi-streaming.tap.js index e86a48746d..07682c0d69 100644 --- a/test/versioned/grpc/client-bidi-streaming.tap.js +++ b/test/versioned/grpc/client-bidi-streaming.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') const { ERR_CODE, ERR_MSG } = require('./constants.cjs') const { @@ -41,11 +42,7 @@ tap.test('gRPC Client: Bidi Streaming', (t) => { client.close() grpc = null proto = null - Object.keys(require.cache).forEach((key) => { - if (key.includes('@grpc/grpc-js')) { - delete require.cache[key] - } - }) + removeModules(['@grpc/grpc-js']) }) t.test( diff --git a/test/versioned/grpc/client-server-streaming.tap.js b/test/versioned/grpc/client-server-streaming.tap.js index e567bd0b5b..b9659b84e9 100644 --- a/test/versioned/grpc/client-server-streaming.tap.js +++ b/test/versioned/grpc/client-server-streaming.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') const { ERR_CODE, ERR_MSG } = require('./constants.cjs') const { @@ -41,11 +42,7 @@ tap.test('gRPC Client: Server Streaming', (t) => { client.close() grpc = null proto = null - Object.keys(require.cache).forEach((key) => { - if (key.includes('@grpc/grpc-js')) { - delete require.cache[key] - } - }) + removeModules(['@grpc/grpc-js']) }) t.test('should track server streaming requests as an external when in a transaction', (t) => { diff --git a/test/versioned/grpc/client-streaming.tap.js b/test/versioned/grpc/client-streaming.tap.js index 6ec7d78749..fcc2c60905 100644 --- a/test/versioned/grpc/client-streaming.tap.js +++ b/test/versioned/grpc/client-streaming.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') const { ERR_CODE, ERR_MSG, HALT_CODE, HALT_SERVER_ERR_MSG } = require('./constants.cjs') const { @@ -41,11 +42,7 @@ tap.test('gRPC Client: Client Streaming', (t) => { client.close() grpc = null proto = null - Object.keys(require.cache).forEach((key) => { - if (key.includes('@grpc/grpc-js')) { - delete require.cache[key] - } - }) + removeModules(['@grpc/grpc-js']) }) t.test('should track client streaming requests as an external when in a transaction', (t) => { diff --git a/test/versioned/grpc/client-unary.tap.js b/test/versioned/grpc/client-unary.tap.js index 99cd9d977e..314deadd03 100644 --- a/test/versioned/grpc/client-unary.tap.js +++ b/test/versioned/grpc/client-unary.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') const { ERR_CODE, ERR_MSG } = require('./constants.cjs') const { @@ -41,11 +42,7 @@ tap.test('gRPC Client: Unary Requests', (t) => { client.close() grpc = null proto = null - Object.keys(require.cache).forEach((key) => { - if (key.includes('@grpc/grpc-js')) { - delete require.cache[key] - } - }) + removeModules(['@grpc/grpc-js']) }) t.test('should track unary client requests as an external when in a transaction', (t) => { diff --git a/test/versioned/grpc/server-bidi-streaming.tap.js b/test/versioned/grpc/server-bidi-streaming.tap.js index 98503652e0..e9fc21c117 100644 --- a/test/versioned/grpc/server-bidi-streaming.tap.js +++ b/test/versioned/grpc/server-bidi-streaming.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') const DESTINATIONS = require('../../../lib/config/attribute-filter').DESTINATIONS const DESTINATION = DESTINATIONS.TRANS_EVENT | DESTINATIONS.ERROR_EVENT const { ERR_CODE, ERR_SERVER_MSG } = require('./constants.cjs') @@ -45,11 +46,7 @@ tap.test('gRPC Server: Bidi Streaming', (t) => { client.close() grpc = null proto = null - Object.keys(require.cache).forEach((key) => { - if (key.includes('@grpc/grpc-js')) { - delete require.cache[key] - } - }) + removeModules(['@grpc/grpc-js']) }) t.test('should track bidirectional requests', async (t) => { diff --git a/test/versioned/grpc/server-client-streaming.tap.js b/test/versioned/grpc/server-client-streaming.tap.js index 161c9cfefc..768335c8ff 100644 --- a/test/versioned/grpc/server-client-streaming.tap.js +++ b/test/versioned/grpc/server-client-streaming.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') const DESTINATIONS = require('../../../lib/config/attribute-filter').DESTINATIONS const DESTINATION = DESTINATIONS.TRANS_EVENT | DESTINATIONS.ERROR_EVENT const { ERR_CODE, ERR_SERVER_MSG, HALT_CODE, HALT_GRPC_SERVER_MSG } = require('./constants.cjs') @@ -45,11 +46,7 @@ tap.test('gRPC Server: Client Streaming', (t) => { client.close() grpc = null proto = null - Object.keys(require.cache).forEach((key) => { - if (key.includes('@grpc/grpc-js')) { - delete require.cache[key] - } - }) + removeModules(['@grpc/grpc-js']) }) t.test('should track client streaming requests', async (t) => { diff --git a/test/versioned/grpc/server-streaming.tap.js b/test/versioned/grpc/server-streaming.tap.js index eb8c7c00b1..606a71c20a 100644 --- a/test/versioned/grpc/server-streaming.tap.js +++ b/test/versioned/grpc/server-streaming.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') const DESTINATIONS = require('../../../lib/config/attribute-filter').DESTINATIONS const DESTINATION = DESTINATIONS.TRANS_EVENT | DESTINATIONS.ERROR_EVENT const { ERR_CODE, ERR_SERVER_MSG } = require('./constants.cjs') @@ -45,11 +46,7 @@ tap.test('gRPC Server: Server Streaming', (t) => { client.close() grpc = null proto = null - Object.keys(require.cache).forEach((key) => { - if (key.includes('@grpc/grpc-js')) { - delete require.cache[key] - } - }) + removeModules(['@grpc/grpc-js']) }) t.test('should track server-streaming requests', async (t) => { diff --git a/test/versioned/grpc/server-unary.tap.js b/test/versioned/grpc/server-unary.tap.js index 9dc9e5027e..e1d8beafae 100644 --- a/test/versioned/grpc/server-unary.tap.js +++ b/test/versioned/grpc/server-unary.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') const DESTINATIONS = require('../../../lib/config/attribute-filter').DESTINATIONS const DESTINATION = DESTINATIONS.TRANS_EVENT | DESTINATIONS.ERROR_EVENT const { ERR_CODE, ERR_SERVER_MSG } = require('./constants.cjs') @@ -45,11 +46,7 @@ tap.test('gRPC Server: Unary Requests', (t) => { client.close() grpc = null proto = null - Object.keys(require.cache).forEach((key) => { - if (key.includes('@grpc/grpc-js')) { - delete require.cache[key] - } - }) + removeModules(['@grpc/grpc-js']) }) t.test('should track unary server requests', async (t) => { diff --git a/test/versioned/ioredis/ioredis-3.tap.js b/test/versioned/ioredis/ioredis-3.tap.js index 932fe33ea7..3bbbe993da 100644 --- a/test/versioned/ioredis/ioredis-3.tap.js +++ b/test/versioned/ioredis/ioredis-3.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeMatchedModules } = require('../../lib/cache-buster') require('../../lib/metrics_helper') const params = require('../../lib/params') @@ -126,13 +127,6 @@ async function setup(t) { } function clearLoadedModules(t) { - let deletedCount = 0 - Object.keys(require.cache).forEach((key) => { - if (key.indexOf('/ioredis/node_modules/ioredis/') >= 0) { - delete require.cache[key] - deletedCount++ - } - }) - + const deletedCount = removeMatchedModules(/ioredis\/node_modules\/ioredis/) t.comment(`Cleared ${deletedCount} modules matching '*/ioredis/node_modules/ioredis/*'`) } diff --git a/test/versioned/langchain/runnables-streaming.tap.js b/test/versioned/langchain/runnables-streaming.tap.js index 6b86bb2424..118c32e6a4 100644 --- a/test/versioned/langchain/runnables-streaming.tap.js +++ b/test/versioned/langchain/runnables-streaming.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') // load the assertSegments assertion require('../../lib/metrics_helper') const { filterLangchainEvents, filterLangchainEventsByType } = require('./common') @@ -48,11 +49,7 @@ async function afterEach(t) { t.context?.server?.close() helper.unloadAgent(t.context.agent) // bust the require-cache so it can re-instrument - Object.keys(require.cache).forEach((key) => { - if (key.includes('@langchain/core') || key.includes('openai')) { - delete require.cache[key] - } - }) + removeModules(['@langchain/core', 'openai']) } tap.test('Langchain instrumentation - chain streaming', (t) => { diff --git a/test/versioned/langchain/runnables.tap.js b/test/versioned/langchain/runnables.tap.js index a1591f2e7b..e6ced1cc4c 100644 --- a/test/versioned/langchain/runnables.tap.js +++ b/test/versioned/langchain/runnables.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') // load the assertSegments assertion require('../../lib/metrics_helper') const { filterLangchainEvents, filterLangchainEventsByType } = require('./common') @@ -46,11 +47,7 @@ tap.test('Langchain instrumentation - runnable sequence', (t) => { t.context?.server?.close() helper.unloadAgent(t.context.agent) // bust the require-cache so it can re-instrument - Object.keys(require.cache).forEach((key) => { - if (key.includes('@langchain/core') || key.includes('openai')) { - delete require.cache[key] - } - }) + removeModules(['@langchain/core', 'openai']) }) t.test('should create langchain events for every invoke call', (t) => { diff --git a/test/versioned/langchain/tools.tap.js b/test/versioned/langchain/tools.tap.js index 26490c38ed..baf24f7343 100644 --- a/test/versioned/langchain/tools.tap.js +++ b/test/versioned/langchain/tools.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules, removeMatchedModules } = require('../../lib/cache-buster') // load the assertSegments assertion require('../../lib/metrics_helper') const { version: pkgVersion } = require('@langchain/core/package.json') @@ -32,11 +33,8 @@ tap.test('Langchain instrumentation - tools', (t) => { t.afterEach((t) => { helper.unloadAgent(t.context.agent) // bust the require-cache so it can re-instrument - Object.keys(require.cache).forEach((key) => { - if (key.includes('@langchain/core') || key.endsWith('/helpers/custom-tool.js')) { - delete require.cache[key] - } - }) + removeModules(['@langchain/core']) + removeMatchedModules(/helpers\/custom-tool\.js$/) }) t.test('should create span on successful tools create', (t) => { diff --git a/test/versioned/langchain/vectorstore.tap.js b/test/versioned/langchain/vectorstore.tap.js index 2851d0bdfd..07511626a1 100644 --- a/test/versioned/langchain/vectorstore.tap.js +++ b/test/versioned/langchain/vectorstore.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeModules } = require('../../lib/cache-buster') // load the assertSegments assertion require('../../lib/metrics_helper') const { version: pkgVersion } = require('@langchain/core/package.json') @@ -61,16 +62,7 @@ tap.test('Langchain instrumentation - vectorstore', (t) => { t.context?.server?.close() helper.unloadAgent(t.context.agent) // bust the require-cache so it can re-instrument - Object.keys(require.cache).forEach((key) => { - if ( - key.includes('@langchain/core') || - key.includes('openai') || - key.includes('@elastic') || - key.includes('@langchain/community') - ) { - delete require.cache[key] - } - }) + removeModules(['@langchain/core', 'openai', '@elastic', '@langchain/community']) }) t.test('should create vectorstore events for every similarity search call', (t) => { diff --git a/test/versioned/mysql/helpers.js b/test/versioned/mysql/helpers.js index b85305d064..7e7ffd8430 100644 --- a/test/versioned/mysql/helpers.js +++ b/test/versioned/mysql/helpers.js @@ -4,17 +4,15 @@ */ 'use strict' + const params = require('../../lib/params') +const { removeMatchedModules } = require('../../lib/cache-buster') module.exports = async function setupDb(user, db, table, mysql) { const regex = new RegExp(/mysql/) await createDb(mysql, user, db) await createTable(mysql, user, db, table) - Object.keys(require.cache).forEach((key) => { - if (regex.test(key)) { - delete require.cache[key] - } - }) + removeMatchedModules(regex) } function runCommand(client, cmd) { diff --git a/test/versioned/nestjs/nest.tap.js b/test/versioned/nestjs/nest.tap.js index 19649b7aee..4eb737bead 100644 --- a/test/versioned/nestjs/nest.tap.js +++ b/test/versioned/nestjs/nest.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeMatchedModules } = require('../../lib/cache-buster') const { promisify } = require('node:util') const makeRequest = promisify(helper.makeGetRequest) const { initNestApp, deleteNestApp } = require('./setup') @@ -28,11 +29,7 @@ tap.test('Verify the Nest.js instrumentation', (t) => { t.teardown(async () => { app.close() helper.unloadAgent(agent) - Object.keys(require.cache).forEach((key) => { - if (/test-app/.test(key)) { - delete require.cache[key] - } - }) + removeMatchedModules(/test-app/) await deleteNestApp() }) diff --git a/test/versioned/pino/pino.tap.js b/test/versioned/pino/pino.tap.js index 20cfe829da..af7537fc8d 100644 --- a/test/versioned/pino/pino.tap.js +++ b/test/versioned/pino/pino.tap.js @@ -10,6 +10,7 @@ const { sink, once } = require('pino/test/helper') const split = require('split2') const { truncate } = require('../../../lib/util/application-logging') const helper = require('../../lib/agent_helper') +const { removeMatchedModules } = require('../../lib/cache-buster') const { LOGGING } = require('../../../lib/metrics/names') const { originalMsgAssertion } = require('./helpers') const semver = require('semver') @@ -29,11 +30,7 @@ tap.test('Pino instrumentation', (t) => { } t.beforeEach(async (t) => { - Object.keys(require.cache).forEach((key) => { - if (/pino/.test(key)) { - delete require.cache[key] - } - }) + removeMatchedModules(/pino/) t.context.pino = null t.context.agent = null diff --git a/test/versioned/redis/redis-v4.tap.js b/test/versioned/redis/redis-v4.tap.js index 9ad5d0fe1c..029700223e 100644 --- a/test/versioned/redis/redis-v4.tap.js +++ b/test/versioned/redis/redis-v4.tap.js @@ -8,6 +8,7 @@ const tap = require('tap') const test = tap.test const helper = require('../../lib/agent_helper') +const { removeMatchedModules } = require('../../lib/cache-buster') const params = require('../../lib/params') const urltils = require('../../../lib/util/urltils') @@ -52,11 +53,7 @@ test('Redis instrumentation', function (t) { } // must purge require cache of redis related instrumentation // otherwise it will not re-register on subsequent test runs - Object.keys(require.cache).forEach((key) => { - if (/redis/.test(key)) { - delete require.cache[key] - } - }) + removeMatchedModules(/redis/) }) t.test('should find Redis calls in the transaction trace', function (t) { diff --git a/test/versioned/superagent/async-await.tap.js b/test/versioned/superagent/async-await.tap.js index cfd5ba43b8..5638257277 100644 --- a/test/versioned/superagent/async-await.tap.js +++ b/test/versioned/superagent/async-await.tap.js @@ -7,7 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') - +const { removeModules } = require('../../lib/cache-buster') const EXTERNAL_NAME = /External\/newrelic.com(:443)*\// tap.test('SuperAgent instrumentation with async/await', (t) => { @@ -17,11 +17,7 @@ tap.test('SuperAgent instrumentation with async/await', (t) => { }) t.afterEach((t) => { helper.unloadAgent(t.context.agent) - Object.keys(require.cache).forEach((key) => { - if (key.includes('superagent')) { - delete require.cache[key] - } - }) + removeModules(['superagent']) }) t.test('should maintain transaction context with promises', (t) => { diff --git a/test/versioned/superagent/superagent.tap.js b/test/versioned/superagent/superagent.tap.js index 4f661527fe..f5cbd30c24 100644 --- a/test/versioned/superagent/superagent.tap.js +++ b/test/versioned/superagent/superagent.tap.js @@ -7,7 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') - +const { removeModules } = require('../../lib/cache-buster') const EXTERNAL_NAME = /External\/newrelic.com(:443)*\// tap.test('SuperAgent instrumentation', (t) => { @@ -17,11 +17,7 @@ tap.test('SuperAgent instrumentation', (t) => { }) t.afterEach((t) => { helper.unloadAgent(t.context.agent) - Object.keys(require.cache).forEach((key) => { - if (key.includes('superagent')) { - delete require.cache[key] - } - }) + removeModules(['superagent']) }) t.test('should maintain transaction context with callbacks', (t) => { diff --git a/test/versioned/winston/winston.tap.js b/test/versioned/winston/winston.tap.js index 63e89a4aac..cb98177f38 100644 --- a/test/versioned/winston/winston.tap.js +++ b/test/versioned/winston/winston.tap.js @@ -7,6 +7,7 @@ const tap = require('tap') const helper = require('../../lib/agent_helper') +const { removeMatchedModules } = require('../../lib/cache-buster') const concat = require('concat-stream') require('../../lib/logging-helper') const { Writable } = require('stream') @@ -39,11 +40,7 @@ tap.test('winston instrumentation', (t) => { winston = null // must purge require cache of winston related instrumentation // otherwise it will not re-register on subsequent test runs - Object.keys(require.cache).forEach((key) => { - if (/winston/.test(key)) { - delete require.cache[key] - } - }) + removeMatchedModules(/winston/) /** * since our nr-winston-transport gets registered