From 448b9a836c0c75a58a388d8737ae88387fe5d459 Mon Sep 17 00:00:00 2001 From: Roman Kuznetsov Date: Wed, 4 Sep 2024 00:40:54 +0300 Subject: [PATCH] fix: remove extra warning on unset deprecated option --- lib/core.js | 2 ++ test/option.js | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/core.js b/lib/core.js index 47ac8d0..90fcbf7 100644 --- a/lib/core.js +++ b/lib/core.js @@ -32,6 +32,8 @@ function option({ : defaultValue; } else if (!isDeprecated) { throw new MissingOptionError(locator.name); + } else { + isSetByUser = false; } if (isSetByUser && isDeprecated) { diff --git a/test/option.js b/test/option.js index 37a69d7..69e22b4 100644 --- a/test/option.js +++ b/test/option.js @@ -1,7 +1,18 @@ +const sinon = require('sinon'); const {option} = require('../lib/core'); const {MissingOptionError} = require('../lib/errors'); describe('option', () => { + let consoleWarnStub; + + beforeEach(() => { + consoleWarnStub = sinon.stub(console, 'warn'); + }); + + afterEach(() => { + consoleWarnStub.restore(); + }); + const LAZY_CONFIG = { root: {defaultKey: 'defaultValue'} }; @@ -155,6 +166,18 @@ describe('option', () => { const parser = option({isDeprecated: true}); assert.doesNotThrow(() => parser({}, LAZY_CONFIG), MissingOptionError); }); + + it('should not log warning on deprecated option', () => { + option({isDeprecated: true})({}, LAZY_CONFIG); + + assert.notCalled(consoleWarnStub); + }); + }); + + it('should log warning on deprecated option if option is set', () => { + option({isDeprecated: true})({envVar: 'foo'}, LAZY_CONFIG); + + assert.calledWith(consoleWarnStub, sinon.match('option is deprecated')); }); function testAfterParseCallback(name) {