cljs-test-display icon indicating copy to clipboard operation
cljs-test-display copied to clipboard

Show expected vs actual as pretty diff?

Open lread opened this issue 5 years ago • 3 comments

I wonder if pretty diffs on expected vs actual results on failures might be interesting.

We should probably continue to show the current failure as is but including also including a pretty diff would make some failures much easier to grok.

I'm thinking along the lines of what eftest and kaocha produce.

lread avatar Apr 12 '19 16:04 lread

there is also tool that modifies output of cljs.test: https://github.com/pjstadig/humane-test-output

nenadalm avatar May 16 '19 19:05 nenadalm

Here is how to have diffs in the output using https://github.com/pjstadig/humane-test-output:

(ns app.run-all
  (:require
   [cljs-test-display.core :as display]
   [cljs.test :refer-macros [run-tests] :refer [empty-env]]
   [goog.dom.classlist :as classlist]
   [goog.dom :as gdom]
   [cljs.pprint :as pp]
   [pjstadig.print :as p]
   [pjstadig.util :as util]
   [app.test-test])
  (:import [goog.string StringBuffer]))

(def dummy-env (empty-env))

;; fix bindings (https://github.com/pjstadig/humane-test-output/pull/38)
(set! p/with-pretty-writer
      (fn with-pretty-writer [f]
        (binding [p/*sb* (StringBuffer.)]
          (binding [*out* (pp/get-pretty-writer (StringBufferWriter. p/*sb*))]
            (f)))))

;; reporting with diffs
(set! display/add-fail-node!
      (fn add-fail-node! [m]
        (let [out (binding [cljs.test/*current-env* dummy-env] ;; we don't want `humane-test-output` to modify the env
                    (with-out-str
                      (util/report- (p/convert-event m))))
              node (display/div :test-fail
                                (display/contexts-node)
                                (display/div :fail-body
                                             (when-let [message (:message m)]
                                               (display/div :test-message message))
                                             (display/n :pre {}
                                                        (display/n :code {} out))))
              curr-node (display/current-node)]
          (classlist/add curr-node "has-failures")
          (classlist/add (display/current-node-parent) "has-failures")
          (gdom/appendChild curr-node node))))

(run-tests (cljs-test-display.core/init! "app-testing")
           'app.test-test)

nenadalm avatar Jul 20 '19 13:07 nenadalm

Hi and thanks for the snippet above! I recently tried it out and it seems like the "fix bindings" part is no longer needed.

Here is my version (which also includes a hack to remove the "FAIL IN" part of the humane-test-output report as well as adding the original :expected that keeps the unevaluated symbols for some added context):

(ns app.run-all
  (:require
   [cljs-test-display.core :as display]
   [cljs.test :refer-macros [run-tests] :refer [empty-env]]
   [goog.dom.classlist :as classlist]
   [goog.dom :as gdom]
   [cljs.pprint :as pp]
   [pjstadig.print :as p]
   [pjstadig.util :as util]
   [app.test-test])
  (:import [goog.string StringBuffer]))

(def dummy-env (empty-env))

;; reporting with diffs
(set! display/add-fail-node!
      (fn add-fail-node! [m]
        (let [out (binding [cljs.test/*current-env* dummy-env] ;; we don't want `humane-test-output` to modify the env
                    (with-out-str
                      (util/report- (p/convert-event m))))
              clean-out (->> (str/split out #"\n")
                             (drop-while #(not (str/starts-with? % "expected")))
                             (str/join "\n")
                             (str (with-out-str (pp/pprint (:expected m))) "\n"))
              node (display/div :test-fail
                                (display/contexts-node)
                                (display/div :fail-body
                                             (when-let [message (:message m)]
                                               (display/div :test-message message))
                                             (display/n :pre {}
                                                        (display/n :code {} clean-out))))
              curr-node (display/current-node)]
          (js/console.log clean-out)
          (classlist/add curr-node "has-failures")
          (classlist/add (display/current-node-parent) "has-failures")
          (gdom/appendChild curr-node node))))

(run-tests (cljs-test-display.core/init! "app-testing")
           'app.test-test)
Screenshot 2020-04-24 at 12 13 00

Dexterminator avatar Apr 24 '20 15:04 Dexterminator