Repository: testem/testem
Pull request #1385 (2019-11-13 22:25:27)

adding willDisplay() so subclasses can check that before trying to display()
in case they have side-effects


Patch:
diff --git a/lib/reporters/tap_reporter.js b/lib/reporters/tap_reporter.js
index 0c98873..e4f6581 100644
--- a/lib/reporters/tap_reporter.js
+++ b/lib/reporters/tap_reporter.js
@@ -48,11 +48,23 @@ module.exports = class TapReporter {
     return lines.join('\n');
   }
 
+  /*
+   * Based on current settings in this object, will the given value be
+   * displayed by 'display'? 
+   */
+  willDisplay(result) {
+    let show = !this.silent && !!result && (!this.failsOnly || result.error);
+    return show;
+  }
+
+  /*
+   * Display a formatted message for the result, but only if 
+   * we've configured to do that.
+   */
   display(prefix, result) {
-    if (this.silent || (this.failsOnly && !result.error)) {
-      return;
+    if (this.willDisplay(result)) {
+      this.out.write(displayutils.resultString(this.id++, prefix, result, this.quietLogs));
     }
-    this.out.write(displayutils.resultString(this.id++, prefix, result, this.quietLogs));
   }
 
   finish() {


Test patch:
diff --git a/tests/ci/reporter_tests.js b/tests/ci/reporter_tests.js
index dbf09b9..758360c 100644
--- a/tests/ci/reporter_tests.js
+++ b/tests/ci/reporter_tests.js
@@ -330,6 +330,44 @@ describe('test reporters', function() {
         });
       });
     });
+
+    context('willDisplay', function() {
+      it.only('silent only, no result', function() {
+        config = new Config('ci', {});
+        var reporter = new TapReporter(true, stream, config);
+        assert.equal(reporter.willDisplay(), false);
+      });
+      it.only('silent only, has result with no error', function() {
+        config = new Config('ci', {});
+        var reporter = new TapReporter(true, stream, config);
+        assert.equal(reporter.willDisplay({}), false);
+      });
+      it.only('silent only, has result with error', function() {
+        config = new Config('ci', {});
+        var reporter = new TapReporter(true, stream, config);
+        assert.equal(reporter.willDisplay({error: true}), false);
+      });
+      it.only('not silent, no result', function() {
+        config = new Config('ci', {});
+        var reporter = new TapReporter(false, stream, config);
+        assert.equal(reporter.willDisplay(), false);
+      });
+      it.only('not silent, result w/error', function() {
+        config = new Config('ci', {});
+        var reporter = new TapReporter(false, stream, config);
+        assert.equal(reporter.willDisplay(), false);
+      });
+      it.only('not silent, tap_failed_tests_only, no error', function() {
+        config = new Config('ci', { tap_failed_tests_only: true });
+        var reporter = new TapReporter(false, stream, config);
+        assert.equal(reporter.willDisplay({error: false}), false);
+      });
+      it.only('not silent, tap_failed_tests_only, has error', function() {
+        config = new Config('ci', { tap_failed_tests_only: true });
+        var reporter = new TapReporter(false, stream, config);
+        assert.equal(reporter.willDisplay({error: true}), true);
+      });
+    });
   });
 
   describe('dot reporter', function() {