Fork me on GitHub
#hyperfiddle
<
2022-08-28
>
bbss04:08:17

Hi, I am giving rcf tests a go, works nicely in clj, but won't print outputs in my (shadow) cljs app. I call the hyperfiddle.rcf/enable! fn, but any subsequent calls to tests don't seem to have an effect. Do I need to configure shadow-cljs somehow, is it really necessary to configure the dev-entrypoint as in the read-me?

Geoffrey Gaillard05:08:04

Does it print to the browser console?

bbss06:08:51

Just returns nil and no apparent side-effect

Geoffrey Gaillard07:08:04

Check the value of hyperfiddle.rcf/*enabled* in both clj and cljs REPL.

bbss07:08:30

Both true!

Geoffrey Gaillard08:08:53

If *enabled* is true in both clj and cljs, then tests blocks should run. If they don’t then it’s a bug. Can you see the effect of a println or js/alert?

bbss09:08:20

Yes those run, a bug is what I suspected, not sure what could be causing it. I tried to remove my .shadow-cljs and rebuild from scratch in case something went wrong there but no dice :(

bbss09:08:43

the only thing that comes to mind in this project that does something with logging is taoensso.timbre

1
Geoffrey Gaillard09:08:22

If effects run, there is no reason assertions wouldn’t run too. You can look into hyperfiddle.rcf.reporters. Cljs version, just call js/console.log. I would be surprised if timbre interferes with it.

bbss09:08:37

yeah, I'm thinking perhaps my shadow-cljs build somehow doesn't run tests?

bbss09:08:07

just to be clear, effects in the tests macro don't run.

Dustin Getz17:08:00

likely using two JVMs (shadow jvm doing macroexpansion for cljs is separate from the CLJ repl JVM). for cljs the enabled flag needs to be set before cljs macroexpansion happens in the shadow JVM

Dustin Getz17:08:00

I'm not even sure the :cljs branch of rcf/enable! is doing anything here

(defn enable! [& [v]]
  #?(:clj  (alter-var-root #'*enabled* (constantly (if (some? v) v true)))
     :cljs (set! *enabled* (if (some? v) v true))))
because *enabled* is checked during macroexpansion here, which happens at compile time in the jvm edit: ah, *enabled* is checked once at compile time and then again at runtime from cljs, so to run cljs tests the flag needs to be set at both compile time and runtime
(defmacro tests [& body]
  (let [name (gen-name &form)]
    (cond
      *generate-tests* `(deftest ~name ~@body)
      *enabled*         (if (:js-globals &env)
                          `(do (defn ~name [] ~(impl/tests* &env body))
                               (when *enabled* (cljs.test/run-block (cljs.test/test-var-block* (var ~name) ~name))))
                          (impl/tests* &env body))
      :else             nil)))

Dustin Getz17:08:37

@U2DART3HA can confirm if this is correct - I didn't write it

Dustin Getz17:08:25

I can see an argument for cljs not inspecting *enabled* at compile time, the test structure should always be available at runtime in cljs (and advanced builds can dead code eliminate it)

👍 1
Geoffrey Gaillard07:08:01

It is correct that *enabled* is checked at macroexpansion time and runtime. The requirement is: tests should behave as comment unless RCF is enabled. Macroexpanding tests in cljs even if *enabled* is false would define test functions in the namespace, breaking the premise. Some projects cannot rely on advanced compilation. There might be other solutions to satisfy the requirement, a configuration map could do the job:

{:clj  #{:enabled}
 :cljs #{:enabled :generate-tests}}

bbss05:09:34

so to fix this I also had to connect to the running shadow-cljs nrepl server with cider-connect-clj and require+enable there.