Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show expected vs actual as pretty diff? #5

Open
lread opened this issue Apr 12, 2019 · 3 comments
Open

Show expected vs actual as pretty diff? #5

lread opened this issue Apr 12, 2019 · 3 comments

Comments

@lread
Copy link

lread commented Apr 12, 2019

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.

@nenadalm
Copy link

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

@nenadalm
Copy link

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)

@Dexterminator
Copy link

Dexterminator commented Apr 24, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants