From 00aa1339b61eca78e2a4c52e22cbfdd08deef06a Mon Sep 17 00:00:00 2001 From: John Henry Date: Mon, 13 Nov 2017 13:48:20 -0800 Subject: [PATCH] Add "onFinish" listener to test harness. --- index.js | 8 ++++++++ lib/results.js | 5 ++++- readme.markdown | 4 ++++ test/onFailure.js | 21 +++++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/onFailure.js diff --git a/index.js b/index.js index f3ffcaee..64d2d0c9 100644 --- a/index.js +++ b/index.js @@ -39,6 +39,10 @@ exports = module.exports = (function () { lazyLoad.onFinish = function () { return getHarness().onFinish.apply(this, arguments); }; + + lazyLoad.onFailure = function() { + return getHarness().onFailure.apply(this, arguments); + }; lazyLoad.getHarness = getHarness @@ -134,6 +138,10 @@ function createHarness (conf_) { test.onFinish = function (cb) { results.on('done', cb); }; + + test.onFailure = function (cb) { + results.on('fail', cb); + }; var only = false; test.only = function () { diff --git a/lib/results.js b/lib/results.js index 76ee75de..305ae6bc 100644 --- a/lib/results.js +++ b/lib/results.js @@ -104,7 +104,10 @@ Results.prototype._watch = function (t) { self.count ++; if (res.ok) self.pass ++ - else self.fail ++ + else { + self.fail ++; + self.emit('fail'); + } }); t.on('test', function (st) { self._watch(st) }); diff --git a/readme.markdown b/readme.markdown index 1e4b5eb7..feebabc4 100644 --- a/readme.markdown +++ b/readme.markdown @@ -162,6 +162,10 @@ Generate a new test that will be skipped over. The onFinish hook will get invoked when ALL tape tests have finished right before tape is about to print the test summary. +## test.onFailure(fn) + +The onFailure hook will get invoked whenever any tape tests has failed. + ## t.plan(n) Declare that `n` assertions should be run. `t.end()` will be called diff --git a/test/onFailure.js b/test/onFailure.js new file mode 100644 index 00000000..e8efdbdb --- /dev/null +++ b/test/onFailure.js @@ -0,0 +1,21 @@ +var tap = require("tap"); +var tape = require("../").createHarness(); + +//Because this test passing depends on a failure, +//we must direct the failing output of the inner test +var noop = function(){} +var mockSink = {on:noop, removeListener:noop, emit:noop, end:noop} +tape.createStream().pipe(mockSink); + +tap.test("on failure", { timeout: 1000 }, function(tt) { + tt.plan(1); + + tape("dummy test", function(t) { + t.fail(); + t.end(); + }); + + tape.onFailure(function() { + tt.pass("tape ended"); + }); +});