Fork me on GitHub
#kaocha
<
2020-06-26
>
rickmoynihan09:06:44

Is it intended that in kaocha a clojure.test/testing block doesn’t (appear) to print the context string if it contains no assertions?

rickmoynihan09:06:22

Normally you’d put assertions in… but https://github.com/igrishaev/etaoin implicitly encourages a style of testing where you don’t always explicitly assert. Essentially because browser testing is inherently asynchronous, you end up testing by waiting for predicates to become true, until a timeout is thrown. The timeout exception is then essentially an assertion failure, and the fact the test makes progress is an implicit success. I don’t particularly like this, but the alternative seems to be that you wait for the predicate to be true; and then assert that predicate for clojure.test; but then each assertion is expressed twice. I suppose I could write a trivial macro to tidy this up; but I was surprised to discover testing blocks without a success or failure don’t appear to display.

plexus10:06:07

hmmm, let me see. You're using the :documentation reporter?

plexus10:06:14

the thing is that testing blocks don't emit events, they only change the context

(defmacro testing
  "Adds a new string to the list of testing contexts.  May be nested,
  but must occur inside a test function (deftest)."
  {:added "1.1"}
  [string & body]
  `(binding [*testing-contexts* (conj *testing-contexts* ~string)]
     ~@body))

plexus10:06:00

the actual printing happens in :pass / :error / :fail, so yes this behavior is in a sense expected. clojure.test does the same thing.

plexus10:06:59

(defmethod report :fail [m]
  (with-test-out
    (inc-report-counter :fail)
    (println "\nFAIL in" (testing-vars-str m))
    (when (seq *testing-contexts*) (println (testing-contexts-str)))
    (when-let [message (:message m)] (println message))
    (println "expected:" (pr-str (:expected m)))
    (println "  actual:" (pr-str (:actual m)))))
(this is from clojure.test, notice the (when (seq *testing-contexts*) ...))

plexus10:06:40

somewhat similar in Kaocha

(defmethod doc :pass [m]
  (t/with-test-out
    (doc-print-contexts t/*testing-contexts*)))

(defmethod doc :error [m]
  (t/with-test-out
    (doc-print-contexts t/*testing-contexts*)
    (print (output/colored :red " ERROR"))))

(defmethod doc :kaocha/fail-type [m]
  (t/with-test-out
    (doc-print-contexts t/*testing-contexts*)
    (print (output/colored :red " FAIL"))))

rickmoynihan10:06:36

Yeah it’s the documentation reporter… Makes sense; I was just surprised when none of my tests appeared to run (because etaoin tests) don’t always contain assertions. I’ve hacked it by explicitly adding some (is :succeeded)

rickmoynihan10:06:53

So I guess without an event your hands are tied then?

rickmoynihan10:06:30

I guess I might need to write my own macro

plexus10:06:13

yeah, this is one of the many small annoyances where clojure.test just doesn't give us the affordances to make things better... One day I'll do lambdaisland.test which will be 100% the same API but will have little things like this fixed

dominicm14:06:17

Alex was looking for a new maintainer

plexus07:06:43

oh really? where was this posted?

dominicm07:06:38

Just mentioned here. There were loose plans to spin clojure.test out, similarly to how clojure.spec was.

plexus10:06:37

right now in the doc reporter we need to keep track of which testing contexts we've already printed, so that if you have multiple assertions we don't print them multiple times... instead we could have had :start-context / :end-context

plexus10:06:07

making your own testing macro that delegates to clojure.test/testing but also calls (t/with-test-out (doc-print-contexts t/*testing-contexts*)) should work

👍 3
plexus10:06:16

or to do it cleanly, have it (t/do-report {:type :start-context}), and then add a (defmethod :kaocha.report/doc :start-context [m] (t/with-test-out (doc-print-contexts t/*testing-contexts*)))