From 311593a03da975d2523d5191b830fcaf8f14e220 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Fri, 13 Sep 2024 15:25:17 -0400 Subject: [PATCH] chore: Migrate block of unit tests to `node:test` --- test/unit/db_util.test.js | 86 +++-- test/unit/environment.test.js | 566 ++++++++++++++++----------------- test/unit/error_events.test.js | 280 ++++++++-------- 3 files changed, 455 insertions(+), 477 deletions(-) diff --git a/test/unit/db_util.test.js b/test/unit/db_util.test.js index f6a19b5152..475d873c9b 100644 --- a/test/unit/db_util.test.js +++ b/test/unit/db_util.test.js @@ -1,53 +1,45 @@ /* - * Copyright 2020 New Relic Corporation. All rights reserved. + * Copyright 2024 New Relic Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 */ 'use strict' -const tap = require('tap') -const util = require('../../lib/db/utils') - -tap.test('DB Utilities:', function (t) { - const useParser = util.extractDatabaseChangeFromUse - - t.test('should match single statement use expressions', function (t) { - t.equal(useParser('use test_db;'), 'test_db') - t.equal(useParser('USE INIT'), 'INIT') - t.end() - }) - - t.test('should not be sensitive to ; omission', function (t) { - t.equal(useParser('use test_db'), 'test_db') - t.end() - }) - - t.test('should not be sensitive to extra ;', function (t) { - t.equal(useParser('use test_db;;;;;;'), 'test_db') - t.end() - }) - - t.test('should not be sensitive to extra white space', function (t) { - t.equal(useParser(' use test_db;'), 'test_db') - t.equal(useParser('use test_db;'), 'test_db') - t.equal(useParser(' use test_db;'), 'test_db') - t.equal(useParser('use test_db ;'), 'test_db') - t.equal(useParser('use test_db; '), 'test_db') - t.end() - }) - - t.test('should match backtick expressions', function (t) { - t.equal(useParser('use `test_db`;'), '`test_db`') - t.equal(useParser('use `☃☃☃☃☃☃`;'), '`☃☃☃☃☃☃`') - t.end() - }) - - t.test('should not match malformed use expressions', function (t) { - t.equal(useParser('use cxvozicjvzocixjv`oasidfjaosdfij`;'), null) - t.equal(useParser('use `oasidfjaosdfij`123;'), null) - t.equal(useParser('use `oasidfjaosdfij` 123;'), null) - t.equal(useParser('use \u0001;'), null) - t.equal(useParser('use oasidfjaosdfij 123;'), null) - t.end() - }) - t.end() + +const test = require('node:test') +const assert = require('node:assert') + +const useParser = require('../../lib/db/utils').extractDatabaseChangeFromUse + +test('should match single statement use expressions', () => { + assert.equal(useParser('use test_db;'), 'test_db') + assert.equal(useParser('USE INIT'), 'INIT') +}) + +test('should not be sensitive to ; omission', () => { + assert.equal(useParser('use test_db'), 'test_db') +}) + +test('should not be sensitive to extra ;', () => { + assert.equal(useParser('use test_db;;;;;;'), 'test_db') +}) + +test('should not be sensitive to extra white space', () => { + assert.equal(useParser(' use test_db;'), 'test_db') + assert.equal(useParser('use test_db;'), 'test_db') + assert.equal(useParser(' use test_db;'), 'test_db') + assert.equal(useParser('use test_db ;'), 'test_db') + assert.equal(useParser('use test_db; '), 'test_db') +}) + +test('should match backtick expressions', () => { + assert.equal(useParser('use `test_db`;'), '`test_db`') + assert.equal(useParser('use `☃☃☃☃☃☃`;'), '`☃☃☃☃☃☃`') +}) + +test('should not match malformed use expressions', () => { + assert.equal(useParser('use cxvozicjvzocixjv`oasidfjaosdfij`;'), null) + assert.equal(useParser('use `oasidfjaosdfij`123;'), null) + assert.equal(useParser('use `oasidfjaosdfij` 123;'), null) + assert.equal(useParser('use \u0001;'), null) + assert.equal(useParser('use oasidfjaosdfij 123;'), null) }) diff --git a/test/unit/environment.test.js b/test/unit/environment.test.js index f761eb5059..c75db60576 100644 --- a/test/unit/environment.test.js +++ b/test/unit/environment.test.js @@ -1,369 +1,369 @@ /* - * Copyright 2020 New Relic Corporation. All rights reserved. + * Copyright 2024 New Relic Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 */ 'use strict' -const tap = require('tap') +const test = require('node:test') +const assert = require('node:assert') +const path = require('node:path') +const fs = require('node:fs/promises') +const { spawn } = require('node:child_process') // For consistent results, unset this in case the user had it set in their // environment when testing. delete process.env.NODE_ENV -const path = require('path') -const fs = require('fs/promises') -const spawn = require('child_process').spawn -const environment = require('../../lib/environment') +const { match } = require('../lib/custom-assertions') const { isSupportedVersion } = require('../lib/agent_helper') +const environment = require('../../lib/environment') function find(settings, name) { - const items = settings.filter(function (candidate) { - return candidate[0] === name - }) - - return items[0] && items[0][1] + const items = settings.filter((candidate) => candidate[0] === name) + return items?.[0]?.[1] } -tap.test('the environment scraper', (t) => { - t.autoend() - let settings = null - - t.before(reloadEnvironment) +test.beforeEach(async (ctx) => { + ctx.nr = {} + ctx.nr.settings = await environment.getJSON() +}) - t.test('should allow clearing of the dispatcher', (t) => { - environment.setDispatcher('custom') +test('should allow clearing of the dispatcher', () => { + environment.setDispatcher('custom') - const dispatchers = environment.get('Dispatcher') - t.has(dispatchers, ['custom'], '') + const dispatchers = environment.get('Dispatcher') + assert.deepStrictEqual(dispatchers, ['custom']) - t.doesNotThrow(function () { - environment.clearDispatcher() - }) - t.end() + assert.doesNotThrow(function () { + environment.clearDispatcher() }) +}) - t.test('should allow setting dispatcher version', (t) => { - environment.setDispatcher('custom', '2') +test('should allow setting dispatcher version', () => { + environment.setDispatcher('custom', '2') - let dispatchers = environment.get('Dispatcher') - t.has(dispatchers, ['custom'], '') + let dispatchers = environment.get('Dispatcher') + assert.deepStrictEqual(dispatchers, ['custom']) - dispatchers = environment.get('Dispatcher Version') - t.has(dispatchers, ['2'], '') + dispatchers = environment.get('Dispatcher Version') + assert.deepStrictEqual(dispatchers, ['2']) - t.doesNotThrow(function () { - environment.clearDispatcher() - }) - t.end() + assert.doesNotThrow(function () { + environment.clearDispatcher() }) +}) - t.test('should collect only a single dispatcher', (t) => { - environment.setDispatcher('first') - let dispatchers = environment.get('Dispatcher') - t.has(dispatchers, ['first'], '') +test('should collect only a single dispatcher', () => { + environment.setDispatcher('first') + let dispatchers = environment.get('Dispatcher') + assert.deepStrictEqual(dispatchers, ['first']) - environment.setDispatcher('custom') - dispatchers = environment.get('Dispatcher') - t.has(dispatchers, ['custom'], '') + environment.setDispatcher('custom') + dispatchers = environment.get('Dispatcher') + assert.deepStrictEqual(dispatchers, ['custom']) - t.doesNotThrow(function () { - environment.clearDispatcher() - }) - t.end() + assert.doesNotThrow(function () { + environment.clearDispatcher() }) +}) - t.test('should allow clearing of the framework', (t) => { - environment.setFramework('custom') - environment.setFramework('another') +test('should allow clearing of the framework', () => { + environment.setFramework('custom') + environment.setFramework('another') - const frameworks = environment.get('Framework') - t.has(frameworks, ['custom', 'another'], '') + const frameworks = environment.get('Framework') + assert.deepStrictEqual(frameworks, ['custom', 'another']) - t.doesNotThrow(function () { - environment.clearFramework() - }) - t.end() + assert.doesNotThrow(function () { + environment.clearFramework() }) +}) - t.test('should persist dispatcher between getJSON()s', async (t) => { - environment.setDispatcher('test') - t.has(environment.get('Dispatcher'), ['test']) +test('should persist dispatcher between getJSON()s', async () => { + environment.setDispatcher('test') + assert.deepStrictEqual(environment.get('Dispatcher'), ['test']) - await environment.refresh() - t.has(environment.get('Dispatcher'), ['test']) - t.end() - }) + await environment.refresh() + assert.deepStrictEqual(environment.get('Dispatcher'), ['test']) +}) - t.test('access to settings', (t) => { - t.ok(settings.length > 1, 'should have some settings') - t.ok(find(settings, 'Processors') > 0, 'should find at least one CPU') - t.ok(find(settings, 'OS'), 'should have found an operating system') - t.ok(find(settings, 'OS version'), 'should have found an operating system version') - t.ok(find(settings, 'Architecture'), 'should have found the system architecture') - t.end() - }) +test('access to settings', (t) => { + const { settings } = t.nr + assert.ok(settings.length > 1, 'should have some settings') + assert.ok(find(settings, 'Processors') > 0, 'should find at least one CPU') + assert.ok(find(settings, 'OS'), 'should have found an operating system') + assert.ok(find(settings, 'OS version'), 'should have found an operating system version') + assert.ok(find(settings, 'Architecture'), 'should have found the system architecture') +}) + +test('Node version', (t) => { + const { settings } = t.nr + assert.ok(find(settings, 'Node.js version'), 'should know the Node.js version') +}) + +test('Node environment', () => { + // expected to be run when NODE_ENV is unset + assert.ok(environment.get('NODE_ENV').length === 0, 'should not find a value for NODE_ENV') +}) - t.test('Node version', (t) => { - t.ok(find(settings, 'Node.js version'), 'should know the Node.js version') - t.end() +test('with process.config', (t) => { + const { settings } = t.nr + assert.ok(find(settings, 'npm installed?'), 'should know whether npm was installed with Node.js') + assert.ok( + find(settings, 'OpenSSL support?'), + 'should know whether OpenSSL support was compiled into Node.js' + ) + assert.ok( + find(settings, 'Dynamically linked to OpenSSL?'), + 'should know whether OpenSSL was dynamically linked in' + ) + assert.ok( + find(settings, 'Dynamically linked to Zlib?'), + 'should know whether Zlib was dynamically linked in' + ) + assert.ok(find(settings, 'DTrace support?'), 'should know whether DTrace support was configured') + assert.ok( + find(settings, 'Event Tracing for Windows (ETW) support?'), + 'should know whether Event Tracing for Windows was configured' + ) +}) + +// TODO: remove tests when we drop support for node 18 +test('without process.config', { skip: isSupportedVersion('v19.0.0') }, async (t) => { + t.beforeEach(async (ctx) => { + ctx.nr.conf = { ...process.config } + + /** + * TODO: Augmenting process.config has been deprecated in Node 16. + * When fully disabled we may no-longer be able to test but also may no-longer need to. + * https://nodejs.org/api/deprecations.html#DEP0150 + */ + process.config = null + + ctx.nr.settings = await environment.getJSON() }) - t.test('Node environment', (t) => { - // expected to be run when NODE_ENV is unset - t.ok(environment.get('NODE_ENV').length === 0, 'should not find a value for NODE_ENV') - t.end() + t.afterEach(async (ctx) => { + process.config = { ...ctx.nr.conf } + ctx.nr.settings = await environment.getJSON() }) - t.test('with process.config', (t) => { - t.ok(find(settings, 'npm installed?'), 'should know whether npm was installed with Node.js') - t.ok( + await t.test('assertions without process.config', (t) => { + const { settings } = t.nr + assert.equal( + find(settings, 'npm installed?'), + undefined, + 'should not know whether npm was installed with Node.js' + ) + assert.equal( + find(settings, 'WAF build system installed?'), + undefined, + 'should not know whether WAF was installed with Node.js' + ) + assert.equal( find(settings, 'OpenSSL support?'), - 'should know whether OpenSSL support was compiled into Node.js' + undefined, + 'should not know whether OpenSSL support was compiled into Node.js' ) - t.ok( + assert.equal( find(settings, 'Dynamically linked to OpenSSL?'), - 'should know whether OpenSSL was dynamically linked in' + undefined, + 'Dynamically linked to OpenSSL?' + ) + assert.equal( + find(settings, 'Dynamically linked to V8?'), + undefined, + 'Dynamically linked to V8?' ) - t.ok( + assert.equal( find(settings, 'Dynamically linked to Zlib?'), - 'should know whether Zlib was dynamically linked in' + undefined, + 'Dynamically linked to Zlib?' ) - t.ok(find(settings, 'DTrace support?'), 'should know whether DTrace support was configured') - t.ok( + assert.equal(find(settings, 'DTrace support?'), undefined, 'DTrace support?') + assert.equal( find(settings, 'Event Tracing for Windows (ETW) support?'), - 'should know whether Event Tracing for Windows was configured' + undefined, + 'Event Tracing for Windows (ETW) support?' ) - t.end() - }) - - // TODO: remove tests when we drop support for node 18 - t.test('without process.config', { skip: isSupportedVersion('v19.0.0') }, (t) => { - let conf = null - - t.before(() => { - conf = { ...process.config } - - /** - * TODO: Augmenting process.config has been deprecated in Node 16. - * When fully disabled we may no-longer be able to test but also may no-longer need to. - * https://nodejs.org/api/deprecations.html#DEP0150 - */ - process.config = null - return reloadEnvironment() - }) - - t.teardown(() => { - process.config = { ...conf } - return reloadEnvironment() - }) - - t.test('assertions without process.config', (t) => { - t.notOk( - find(settings, 'npm installed?'), - 'should not know whether npm was installed with Node.js' - ) - t.notOk( - find(settings, 'WAF build system installed?'), - 'should not know whether WAF was installed with Node.js' - ) - t.notOk( - find(settings, 'OpenSSL support?'), - 'should not know whether OpenSSL support was compiled into Node.js' - ) - t.notOk(find(settings, 'Dynamically linked to OpenSSL?'), 'Dynamically linked to OpenSSL?') - t.notOk(find(settings, 'Dynamically linked to V8?'), 'Dynamically linked to V8?') - t.notOk(find(settings, 'Dynamically linked to Zlib?'), 'Dynamically linked to Zlib?') - t.notOk(find(settings, 'DTrace support?'), 'DTrace support?') - t.notOk( - find(settings, 'Event Tracing for Windows (ETW) support?'), - 'Event Tracing for Windows (ETW) support?' - ) - t.end() - }) - t.end() }) +}) - t.test('should have built a flattened package list', (t) => { - const packages = find(settings, 'Packages') - t.ok(packages.length > 5) - packages.forEach((pair) => { - t.equal(JSON.parse(pair).length, 2) - }) - t.end() +test('should have built a flattened package list', (t) => { + const { settings } = t.nr + const packages = find(settings, 'Packages') + assert.ok(packages.length > 5) + packages.forEach((pair) => { + assert.equal(JSON.parse(pair).length, 2) }) +}) - t.test('should have built a flattened dependency list', (t) => { - const dependencies = find(settings, 'Dependencies') - t.ok(dependencies.length > 5) - dependencies.forEach((pair) => { - t.equal(JSON.parse(pair).length, 2) - }) - t.end() +test('should have built a flattened dependency list', (t) => { + const { settings } = t.nr + const dependencies = find(settings, 'Dependencies') + assert.ok(dependencies.length > 5) + dependencies.forEach((pair) => { + assert.equal(JSON.parse(pair).length, 2) }) +}) - t.test('should get correct version for dependencies', async (t) => { - const root = path.join(__dirname, '../lib/example-packages') - const packages = [] - await environment.listPackages(root, packages) - const versions = packages.reduce(function (map, pkg) { - map[pkg[0]] = pkg[1] - return map - }, {}) - - t.same(versions, { +test('should get correct version for dependencies', async () => { + const root = path.join(__dirname, '../lib/example-packages') + const packages = [] + await environment.listPackages(root, packages) + const versions = packages.reduce(function (map, pkg) { + map[pkg[0]] = pkg[1] + return map + }, {}) + + assert.equal( + match(versions, { 'invalid-json': '', 'valid-json': '1.2.3' - }) - t.end() - }) - - // TODO: remove this test when we drop support for node 18 - t.test( - 'should resolve refresh where deps and deps of deps are symlinked to each other', - { skip: isSupportedVersion('v19.0.0') }, - async (t) => { - process.config.variables.node_prefix = path.join(__dirname, '../lib/example-deps') - const data = await environment.getJSON() - const pkgs = find(data, 'Dependencies') - const customPkgs = pkgs.filter((pkg) => pkg.includes('custom-pkg')) - t.equal(customPkgs.length, 3) - t.end() - } + }), + true ) +}) - t.test('should not crash when given a file in NODE_PATH', (t) => { - const env = { - NODE_PATH: path.join(__dirname, 'environment.test.js'), - PATH: process.env.PATH - } +// TODO: remove this test when we drop support for node 18 +test( + 'should resolve refresh where deps and deps of deps are symlinked to each other', + { skip: isSupportedVersion('v19.0.0') }, + async () => { + process.config.variables.node_prefix = path.join(__dirname, '../lib/example-deps') + const data = await environment.getJSON() + const pkgs = find(data, 'Dependencies') + const customPkgs = pkgs.filter((pkg) => pkg.includes('custom-pkg')) + assert.equal(customPkgs.length, 3) + } +) - const opt = { - env: env, - stdio: 'inherit', - cwd: path.join(__dirname, '..') - } +test('should not crash when given a file in NODE_PATH', (t, end) => { + const env = { + NODE_PATH: path.join(__dirname, 'environment.test.js'), + PATH: process.env.PATH + } - const exec = process.argv[0] - const args = [path.join(__dirname, '../helpers/environment.child.js')] - const proc = spawn(exec, args, opt) + const opt = { + env: env, + stdio: 'inherit', + cwd: path.join(__dirname, '..') + } - proc.on('exit', function (code) { - t.equal(code, 0) - t.end() - }) + const exec = process.argv[0] + const args = [path.join(__dirname, '../helpers/environment.child.js')] + const proc = spawn(exec, args, opt) + + proc.on('exit', function (code) { + assert.equal(code, 0) + end() }) +}) - t.test('with symlinks', (t) => { - t.autoend() - const nmod = path.resolve(__dirname, '../helpers/node_modules') - const makeDir = (dirp) => { - try { - return fs.mkdir(dirp) - } catch (err) { - if (err.code !== 'EEXIST') { - return err - } - return null +test('with symlinks', async (t) => { + const nmod = path.resolve(__dirname, '../helpers/node_modules') + + function makeDir(dirp) { + try { + return fs.mkdir(dirp) + } catch (error) { + if (error.code !== 'EEXIST') { + return error } + return null } - const makePackage = async (pkg, dep) => { - const dir = path.join(nmod, pkg) + } - // Make the directory tree. - await makeDir(dir) // make the directory - await makeDir(path.join(dir, 'node_modules')) // make the modules subdirectory + async function makePackage(pkg, dep) { + const dir = path.join(nmod, pkg) - // Make the package.json - const pkgJSON = { name: pkg, dependencies: {} } - pkgJSON.dependencies[dep] = '*' - await fs.writeFile(path.join(dir, 'package.json'), JSON.stringify(pkgJSON)) + // Make the directory tree. + await makeDir(dir) // make the directory + await makeDir(path.join(dir, 'node_modules')) // make the modules subdirectory - // Make the dep a symlink. - const depModule = path.join(dir, 'node_modules', dep) - return fs.symlink(path.join(nmod, dep), depModule, 'dir') - } + // Make the package.json + const pkgJSON = { name: pkg, dependencies: {} } + pkgJSON.dependencies[dep] = '*' + await fs.writeFile(path.join(dir, 'package.json'), JSON.stringify(pkgJSON)) - t.beforeEach(async () => { - await fs.access(nmod).catch(async () => { - await fs.mkdir(nmod) - }) + // Make the dep a symlink. + const depModule = path.join(dir, 'node_modules', dep) + return fs.symlink(path.join(nmod, dep), depModule, 'dir') + } - // node_modules/ - // a/ - // package.json - // node_modules/ - // b (symlink) - // b/ - // package.json - // node_modules/ - // a (symlink) - await makePackage('a', 'b') - await makePackage('b', 'a') - }) + function execChild(cb) { + const opt = { + stdio: 'pipe', + env: process.env, + cwd: path.join(__dirname, '../helpers') + } - t.afterEach(async () => { - const aDir = path.join(nmod, 'a') - const bDir = path.join(nmod, 'b') - await fs.rm(aDir, { recursive: true, force: true }) - await fs.rm(bDir, { recursive: true, force: true }) - }) + const exec = process.argv[0] + const args = [path.join(__dirname, '../helpers/environment.child.js')] + const proc = spawn(exec, args, opt) - t.test('should not crash when encountering a cyclical symlink', (t) => { - execChild((code) => { - t.equal(code, 0) - t.end() - }) - }) + proc.stdout.pipe(process.stderr) + proc.stderr.pipe(process.stderr) - t.test('should not crash when encountering a dangling symlink', async (t) => { - await fs.rm(path.join(nmod, 'a'), { recursive: true, force: true }) - return new Promise((resolve) => { - execChild((code) => { - t.equal(code, 0) - resolve() - }) - }) + proc.on('exit', (code) => { + cb(code) }) + } - function execChild(cb) { - const opt = { - stdio: 'pipe', - env: process.env, - cwd: path.join(__dirname, '../helpers') - } - - const exec = process.argv[0] - const args = [path.join(__dirname, '../helpers/environment.child.js')] - const proc = spawn(exec, args, opt) - - proc.stdout.pipe(process.stderr) - proc.stderr.pipe(process.stderr) + t.beforeEach(async () => { + await fs.access(nmod).catch(async () => await fs.mkdir(nmod)) + + // node_modules/ + // a/ + // package.json + // node_modules/ + // b (symlink) + // b/ + // package.json + // node_modules/ + // a (symlink) + await makePackage('a', 'b') + await makePackage('b', 'a') + }) - proc.on('exit', (code) => { - cb(code) - }) - } + t.afterEach(async () => { + const aDir = path.join(nmod, 'a') + const bDir = path.join(nmod, 'b') + await fs.rm(aDir, { recursive: true, force: true }) + await fs.rm(bDir, { recursive: true, force: true }) }) - t.test('when NODE_ENV is "production"', async (t) => { - process.env.NODE_ENV = 'production' + await t.test('should not crash when encountering a cyclical symlink', (t, end) => { + execChild((code) => { + assert.equal(code, 0) + end() + }) + }) - t.teardown(() => { - delete process.env.NODE_ENV + await t.test('should not crash when encountering a dangling symlink', async () => { + await fs.rm(path.join(nmod, 'a'), { recursive: true, force: true }) + await new Promise((resolve) => { + execChild((code) => { + assert.equal(code, 0) + resolve() + }) }) + }) +}) - const nSettings = await environment.getJSON() +test('when NODE_ENV is "production"', async (t) => { + process.env.NODE_ENV = 'production' - t.equal( - find(nSettings, 'NODE_ENV'), - 'production', - `should save the NODE_ENV value in the environment settings` - ) - t.end() + t.after(() => { + delete process.env.NODE_ENV }) - async function reloadEnvironment() { - settings = await environment.getJSON() - } + const nSettings = await environment.getJSON() + + assert.equal( + find(nSettings, 'NODE_ENV'), + 'production', + `should save the NODE_ENV value in the environment settings` + ) }) diff --git a/test/unit/error_events.test.js b/test/unit/error_events.test.js index f9eac2e6b9..3f18426c10 100644 --- a/test/unit/error_events.test.js +++ b/test/unit/error_events.test.js @@ -1,180 +1,166 @@ /* - * Copyright 2020 New Relic Corporation. All rights reserved. + * Copyright 2024 New Relic Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 */ 'use strict' -const tap = require('tap') -const test = tap.test +const test = require('node:test') +const assert = require('node:assert') const helper = require('../lib/agent_helper') -const Exception = require('../../lib/errors').Exception +const { Exception } = require('../../lib/errors') -test('Error events', (t) => { - t.autoend() - - t.test('when error events are disabled', (t) => { - let agent - - t.beforeEach(() => { - agent = helper.loadMockedAgent() - }) - - t.afterEach(() => { - helper.unloadAgent(agent) - }) - - t.test('collector can override', (t) => { - agent.config.error_collector.capture_events = false - t.doesNotThrow(() => - agent.config.onConnect({ - 'error_collector.capture_events': true, - 'error_collector.max_event_samples_stored': 42 - }) - ) - t.equal(agent.config.error_collector.capture_events, true) - t.equal(agent.config.error_collector.max_event_samples_stored, 42) - - t.end() - }) +test('when error events are disabled', async (t) => { + t.beforeEach((ctx) => { + ctx.nr = {} + ctx.nr.agent = helper.loadMockedAgent() + }) - t.end() + t.afterEach((ctx) => { + helper.unloadAgent(ctx.nr.agent) }) - t.test('attributes', (t) => { - let agent + await t.test('collector can override', (t) => { + const { agent } = t.nr + agent.config.error_collector.capture_events = false + assert.doesNotThrow(() => + agent.config.onConnect({ + 'error_collector.capture_events': true, + 'error_collector.max_event_samples_stored': 42 + }) + ) + assert.equal(agent.config.error_collector.capture_events, true) + assert.equal(agent.config.error_collector.max_event_samples_stored, 42) + }) +}) - t.beforeEach(() => { - agent = helper.loadMockedAgent() - }) +test('attributes', async (t) => { + t.beforeEach((ctx) => { + ctx.nr = {} + ctx.nr.agent = helper.loadMockedAgent() + }) - t.afterEach(() => { - helper.unloadAgent(agent) - }) + t.afterEach((ctx) => { + helper.unloadAgent(ctx.nr.agent) + }) - t.test('should include DT intrinsics', (t) => { - agent.config.distributed_tracing.enabled = true - agent.config.primary_application_id = 'test' - agent.config.account_id = 1 - helper.runInTransaction(agent, function (tx) { - const payload = tx._createDistributedTracePayload().text() - tx.isDistributedTrace = null - tx._acceptDistributedTracePayload(payload) - const error = new Error('some error') - const customAttributes = {} - const timestamp = 0 - const exception = new Exception({ error, customAttributes, timestamp }) - tx.addException(exception) - - tx.end() - const attributes = agent.errors.eventAggregator.getEvents()[0][0] - - t.equal(attributes.type, 'TransactionError') - t.equal(attributes.traceId, tx.traceId) - t.equal(attributes.guid, tx.id) - t.equal(attributes.priority, tx.priority) - t.equal(attributes.sampled, tx.sampled) - t.equal(attributes['parent.type'], 'App') - t.equal(attributes['parent.app'], agent.config.primary_application_id) - t.equal(attributes['parent.account'], agent.config.account_id) - t.equal(attributes['nr.transactionGuid'], tx.id) - t.notOk(attributes.parentId) - t.notOk(attributes.parentSpanId) - - t.end() - }) + await t.test('should include DT intrinsics', (t, end) => { + const { agent } = t.nr + agent.config.distributed_tracing.enabled = true + agent.config.primary_application_id = 'test' + agent.config.account_id = 1 + helper.runInTransaction(agent, function (tx) { + const payload = tx._createDistributedTracePayload().text() + tx.isDistributedTrace = null + tx._acceptDistributedTracePayload(payload) + const error = new Error('some error') + const customAttributes = {} + const timestamp = 0 + const exception = new Exception({ error, customAttributes, timestamp }) + tx.addException(exception) + + tx.end() + const attributes = agent.errors.eventAggregator.getEvents()[0][0] + + assert.equal(attributes.type, 'TransactionError') + assert.equal(attributes.traceId, tx.traceId) + assert.equal(attributes.guid, tx.id) + assert.equal(attributes.priority, tx.priority) + assert.equal(attributes.sampled, tx.sampled) + assert.equal(attributes['parent.type'], 'App') + assert.equal(attributes['parent.app'], agent.config.primary_application_id) + assert.equal(attributes['parent.account'], agent.config.account_id) + assert.equal(attributes['nr.transactionGuid'], tx.id) + assert.equal(attributes.parentId, undefined) + assert.equal(attributes.parentSpanId, undefined) + + end() }) + }) - t.test('should include spanId agent attribute', (t) => { - agent.config.distributed_tracing.enabled = true - agent.config.primary_application_id = 'test' - agent.config.account_id = 1 - helper.runInTransaction(agent, function (tx) { - const payload = tx._createDistributedTracePayload().text() - tx.isDistributedTrace = null - tx._acceptDistributedTracePayload(payload) - const error = new Error('some error') - const customAttributes = {} - const timestamp = 0 - const exception = new Exception({ error, customAttributes, timestamp }) - tx.addException(exception) - - const segment = tx.agent.tracer.getSegment() + await t.test('should include spanId agent attribute', (t, end) => { + const { agent } = t.nr + agent.config.distributed_tracing.enabled = true + agent.config.primary_application_id = 'test' + agent.config.account_id = 1 + helper.runInTransaction(agent, function (tx) { + const payload = tx._createDistributedTracePayload().text() + tx.isDistributedTrace = null + tx._acceptDistributedTracePayload(payload) + const error = new Error('some error') + const customAttributes = {} + const timestamp = 0 + const exception = new Exception({ error, customAttributes, timestamp }) + tx.addException(exception) - tx.end() + const segment = tx.agent.tracer.getSegment() - const { 2: agentAttributes } = agent.errors.eventAggregator.getEvents()[0] + tx.end() - t.equal(agentAttributes.spanId, segment.id) + const { 2: agentAttributes } = agent.errors.eventAggregator.getEvents()[0] - t.end() - }) - }) + assert.equal(agentAttributes.spanId, segment.id) - t.test('should have the expected priority', (t) => { - agent.config.distributed_tracing.enabled = true - agent.config.primary_application_id = 'test' - agent.config.account_id = 1 - helper.runInTransaction(agent, function (tx) { - const error = new Error('some error') - const customAttributes = {} - const timestamp = 0 - const exception = new Exception({ error, customAttributes, timestamp }) - tx.addException(exception) - tx.end() - const attributes = agent.errors.eventAggregator.getEvents()[0][0] - - t.equal(attributes.type, 'TransactionError') - t.equal(attributes.traceId, tx.traceId) - t.equal(attributes.guid, tx.id) - t.equal(attributes.priority, tx.priority) - t.equal(attributes.sampled, tx.sampled) - t.equal(attributes['nr.transactionGuid'], tx.id) - t.ok(tx.priority > 1) - t.equal(tx.sampled, true) - - t.end() - }) + end() }) - - t.end() }) - t.test('when error events are enabled', (t) => { - let agent - - t.beforeEach(() => { - agent = helper.loadMockedAgent() - agent.config.error_collector.capture_events = true - }) - - t.afterEach(() => { - helper.unloadAgent(agent) - }) - - t.test('collector can override', (t) => { - t.doesNotThrow(() => agent.config.onConnect({ 'error_collector.capture_events': false })) - t.equal(agent.config.error_collector.capture_events, false) - - t.end() + await t.test('should have the expected priority', (t, end) => { + const { agent } = t.nr + agent.config.distributed_tracing.enabled = true + agent.config.primary_application_id = 'test' + agent.config.account_id = 1 + helper.runInTransaction(agent, function (tx) { + const error = new Error('some error') + const customAttributes = {} + const timestamp = 0 + const exception = new Exception({ error, customAttributes, timestamp }) + tx.addException(exception) + tx.end() + const attributes = agent.errors.eventAggregator.getEvents()[0][0] + + assert.equal(attributes.type, 'TransactionError') + assert.equal(attributes.traceId, tx.traceId) + assert.equal(attributes.guid, tx.id) + assert.equal(attributes.priority, tx.priority) + assert.equal(attributes.sampled, tx.sampled) + assert.equal(attributes['nr.transactionGuid'], tx.id) + assert.ok(tx.priority > 1) + assert.equal(tx.sampled, true) + + end() }) + }) +}) - t.test('collector can disable using the emergency shut off', (t) => { - t.doesNotThrow(() => agent.config.onConnect({ collect_error_events: false })) - t.equal(agent.config.error_collector.capture_events, false) +test('attributes', async (t) => { + t.beforeEach((ctx) => { + ctx.nr = {} + ctx.nr.agent = helper.loadMockedAgent() + ctx.nr.agent.config.error_collector.capture_events = true + }) - t.end() - }) + t.afterEach((ctx) => { + helper.unloadAgent(ctx.nr.agent) + }) - t.test('collector cannot enable using the emergency shut off', (t) => { - agent.config.error_collector.capture_events = false - t.doesNotThrow(() => agent.config.onConnect({ collect_error_events: true })) - t.equal(agent.config.error_collector.capture_events, false) + await t.test('collector can override', (t) => { + const { agent } = t.nr + assert.doesNotThrow(() => agent.config.onConnect({ 'error_collector.capture_events': false })) + assert.equal(agent.config.error_collector.capture_events, false) + }) - t.end() - }) + await t.test('collector can disable using the emergency shut off', (t) => { + const { agent } = t.nr + assert.doesNotThrow(() => agent.config.onConnect({ collect_error_events: false })) + assert.equal(agent.config.error_collector.capture_events, false) + }) - t.end() + await t.test('collector cannot enable using the emergency shut off', (t) => { + const { agent } = t.nr + agent.config.error_collector.capture_events = false + assert.doesNotThrow(() => agent.config.onConnect({ collect_error_events: true })) + assert.equal(agent.config.error_collector.capture_events, false) }) })