Skip to content

Commit 4726ea9

Browse files
vemvbbatsov
authored andcommitted
Introduce middleware.test/*test-error-handler*
This allows users to attach arbitrary functionality when tests fail due to an exception. For example, I used this to add an Expound-backed `println` for exceptions that were caused by Spec failures. ...While CIDER already prints Spec failures nicely via an Emacs UI, not all cider-nrepl clients necessarily have such an UI. One might also appreciate a println with Expound's format, which is familiar for many, and will be consistent with printlns placed elsewhere in a given codebase.
1 parent 6245a9e commit 4726ea9

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New features
66

77
* [#698](https://github.com/clojure-emacs/cider-nrepl/pull/698): Add `undef-all` op to undefine all symbols and aliases in namespace
8+
* Introduce `cider.nrepl.middleware.test/*test-error-handler*` var which you can override with arbitrary functions.
89

910
### Bugs fixed
1011

src/cider/nrepl/middleware/test.clj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@
7373
println)]
7474
(with-out-str (print-fn object))))
7575

76+
(def ^:dynamic *test-error-handler*
77+
"A function you can override via `binding`, or safely via `alter-var-root`.
78+
On test `:error`s, the related Throwable be invoked as the sole argument
79+
passed to this var.
80+
For example, you can use this to add an additional `println`,
81+
for pretty-printing Spec failures. Remember to `flush` if doing so."
82+
identity)
83+
7684
(defn test-result
7785
"Transform the result of a test assertion. Append ns, var, assertion index,
7886
and 'testing' context. Retain any exception. Pretty-print expected/actual or
@@ -81,7 +89,7 @@
8189
(let [{:keys [actual diffs expected fault]
8290
t :type} m
8391
c (when (seq test/*testing-contexts*) (test/testing-contexts-str))
84-
i (count (get-in (@current-report :results) [ns (:name (meta v))]))
92+
i (count (get-in (:results @current-report {}) [ns (:name (meta v))]))
8593
gen-input (:gen-input @current-report)]
8694

8795
;; Errors outside assertions (faults) do not return an :expected value.
@@ -99,6 +107,7 @@
99107
(when (#{:error} t)
100108
(let [e actual
101109
f (or (:test (meta v)) @v)] ; test fn or deref'ed fixture
110+
(*test-error-handler* e)
102111
{:error e
103112
:line (:line (stack-frame e f))})))))
104113

test/clj/cider/nrepl/middleware/test_test.clj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,17 @@
151151
(#'test/print-object (with-meta {:not :printed} {:type ::custom}))))
152152
(is (= "{:a :b, :c :d}\n"
153153
(#'test/print-object {:a :b :c :d})))))
154+
155+
(deftest test-result-test
156+
(testing "It passes `:error`s to `test/*test-error-handler*`"
157+
(let [proof (atom [])
158+
exception (ex-info "." {::unique (rand)})]
159+
(binding [test/*test-error-handler* (fn [e]
160+
(swap! proof conj e))]
161+
(with-out-str
162+
(test/test-result 'some-ns
163+
#'+
164+
{:type :error
165+
:actual exception}))
166+
(is (= [exception]
167+
@proof))))))

0 commit comments

Comments
 (0)