From 45dfffa407bcda8e93efa065f7863ee277620dee Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Tue, 6 Aug 2024 11:06:28 -0700 Subject: [PATCH] expose spy-setting method --- lib/stub-logger.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/stub-logger.js b/lib/stub-logger.js index 9564c84..aa053a9 100644 --- a/lib/stub-logger.js +++ b/lib/stub-logger.js @@ -7,12 +7,15 @@ class StubLogger { assert.strictEqual(sinon?.constructor?.name, 'Sandbox', 'sinon dependency not recognized'); this.sinon = sinon; const logger = (process.env.VERBOSE_TESTS || verbose) ? console : this.constructor._nullLogger; - Object.keys(this.constructor._nullLogger).forEach((level) => { - this[level] = logger[level]; // eslint-disable-line security/detect-object-injection - this.sinon.spy(this, level); - }); + // eslint-disable-next-line security/detect-object-injection + this.constructor._levels.forEach((level) => this[level] = logger[level]); + this._spy(); } + /** + * Default log levels are nop. + * @returns {object} nop log levels + */ static get _nullLogger() { return { error: () => undefined, @@ -23,10 +26,24 @@ class StubLogger { }; } + /** + * Returns names of all log levels. + * @returns {string[]} list of log level names + */ static get _levels() { return Object.keys(this._nullLogger); } + /** + * Install spies on all log levels. + */ + _spy() { + this.constructor._levels.forEach((level) => this.sinon.spy(this, level)); + } + + /** + * Reset spies on all log levels. + */ _reset() { this.constructor._levels.forEach((level) => this[level].resetHistory()); // eslint-disable-line security/detect-object-injection } -- 2.45.2