expose spy-setting method
authorJustin Wind <justin.wind+git@gmail.com>
Tue, 6 Aug 2024 18:06:28 +0000 (11:06 -0700)
committerJustin Wind <justin.wind+git@gmail.com>
Tue, 6 Aug 2024 18:06:28 +0000 (11:06 -0700)
lib/stub-logger.js

index 9564c843d45659c46581f83ed5b14f7b0d1f87cc..aa053a9eeeab801f2aca49e45a992c25e78fb9b7 100644 (file)
@@ -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
   }