Skip to content

Commit c3d6b69

Browse files
authored
fix: report test assertion line on exception (#729)
Currently, when a clojure test assertion fails with an exception, the :line reported is for the start of the test function. This changes the reported line to the first line of any function within the test function.
1 parent 7dd4e1b commit c3d6b69

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/cider/nrepl/middleware/test.clj

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[cider.nrepl.middleware.util :as util]
88
[cider.nrepl.middleware.util.coerce :as util.coerce]
99
[clojure.pprint :as pp]
10+
[clojure.string :as str]
1011
[clojure.test :as test]
1112
[clojure.walk :as walk]
1213
[nrepl.middleware.interruptible-eval :as ie]
@@ -64,7 +65,10 @@
6465
(->> e
6566
.getStackTrace
6667
(map (partial st/analyze-frame namespaces))
67-
(filter #(= (:class %) class-name))
68+
(filter
69+
#(when-let [frame-cname (:class %)]
70+
(when (string? frame-cname)
71+
(str/starts-with? frame-cname class-name))))
6872
first))))
6973

7074
(defn- print-object

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

+12
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,15 @@
210210
(comparable-stack-frame (test/stack-frame e throws)))
211211
"Returns a map representing the stack frame of the precise function
212212
that threw the exception")))
213+
214+
(deftest stack-frame-line-test
215+
(let [e (try
216+
(throws)
217+
(catch ExceptionInfo e
218+
e))]
219+
;; NOTE this offset is subject to formatting of this test
220+
(is (= (+ 2 (:line (meta #'stack-frame-line-test)))
221+
(:line (test/stack-frame
222+
e
223+
(-> #'stack-frame-line-test meta :test))))
224+
"Returns the line of the exception")))

0 commit comments

Comments
 (0)