From 96f29e4f2a437ce8d90ef75d3811b268ad8c76c2 Mon Sep 17 00:00:00 2001 From: Svetlana Brennan <50715937+svetlanabrennan@users.noreply.github.com> Date: Thu, 30 Jan 2025 15:00:01 -0600 Subject: [PATCH 1/2] wip undefined as default --- lib/config/index.js | 2 +- test/unit/config/config-defaults.test.js | 38 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/config/index.js b/lib/config/index.js index 7ce5befb38..dad6936351 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -1042,7 +1042,7 @@ Config.prototype._fromPassed = function _fromPassed(external, internal, arbitrar Object.keys(external).forEach(function overwrite(key) { // if it's not in the defaults, it doesn't exist - if (!arbitrary && internal[key] === undefined) { + if (!arbitrary && !(key in internal)) { return } diff --git a/test/unit/config/config-defaults.test.js b/test/unit/config/config-defaults.test.js index 61e344771a..af282116ce 100644 --- a/test/unit/config/config-defaults.test.js +++ b/test/unit/config/config-defaults.test.js @@ -10,6 +10,8 @@ const assert = require('node:assert') const path = require('path') const Config = require('../../../lib/config') +const proxyquire = require('proxyquire') +const sinon = require('sinon') test('with default properties', async (t) => { let configuration = null @@ -316,3 +318,39 @@ test('with default properties', async (t) => { assert.equal(configuration.instrumentation.npmlog.enabled, true) }) }) + +test('with undefined as default', async (t) => { + let configuration = null + + const mockConfig = { + agent_control: { + health: { + delivery_location: { + default: undefined + } + } + } + } + + const Config = proxyquire('../../../lib/config/index', { + './default': { + defaultConfig: sinon.stub().returns(mockConfig) + } + }) + + configuration = Config.initialize({ + agent_control: { + enabled: true, + health: { + delivery_location: 'file://find/me', + frequency: 10 + } + } + }) + + assert.strictEqual(configuration.agent_control.health.delivery_location.default, undefined) + + await t.test('should have nest item as find me', () => { + assert.equal(configuration.agent_control.health.delivery_location, 'file://find/me') + }) +}) From 14672bd9a3efbce1147d9c3abc3a8fd7a622919c Mon Sep 17 00:00:00 2001 From: Svetlana Brennan <50715937+svetlanabrennan@users.noreply.github.com> Date: Fri, 31 Jan 2025 11:52:26 -0600 Subject: [PATCH 2/2] fix stubbing in test --- test/unit/config/config-defaults.test.js | 37 ++++++++++-------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/test/unit/config/config-defaults.test.js b/test/unit/config/config-defaults.test.js index af282116ce..0cd04f627d 100644 --- a/test/unit/config/config-defaults.test.js +++ b/test/unit/config/config-defaults.test.js @@ -11,7 +11,6 @@ const path = require('path') const Config = require('../../../lib/config') const proxyquire = require('proxyquire') -const sinon = require('sinon') test('with default properties', async (t) => { let configuration = null @@ -320,37 +319,33 @@ test('with default properties', async (t) => { }) test('with undefined as default', async (t) => { - let configuration = null - const mockConfig = { - agent_control: { - health: { - delivery_location: { + fake_key: { + another_layer: { + fake_nested_key: { default: undefined } } } } - const Config = proxyquire('../../../lib/config/index', { - './default': { - defaultConfig: sinon.stub().returns(mockConfig) - } + const defaults = require('../../../lib/config/default') + const orig = defaults.definition + defaults.definition = function stub() { + const configDefaults = orig.apply(this, arguments) + return { ...configDefaults, ...mockConfig } + } + const Config = proxyquire('../../../lib/config', { + './default': defaults }) - configuration = Config.initialize({ - agent_control: { - enabled: true, - health: { - delivery_location: 'file://find/me', - frequency: 10 + const configuration = Config.initialize({ + fake_key: { + another_layer: { + fake_nested_key: 'fake-value' } } }) - assert.strictEqual(configuration.agent_control.health.delivery_location.default, undefined) - - await t.test('should have nest item as find me', () => { - assert.equal(configuration.agent_control.health.delivery_location, 'file://find/me') - }) + assert.equal(configuration.fake_key.another_layer.fake_nested_key, 'fake-value') })