From: Justin Wind Date: Tue, 6 Aug 2024 18:06:28 +0000 (-0700) Subject: expose spy-setting method X-Git-Tag: v3.0.0~1 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=45dfffa407bcda8e93efa065f7863ee277620dee;p=squeep-test-helper expose spy-setting method --- 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 }