Skip to content

Instantly share code, notes, and snippets.

@athomasoriginal
Last active September 8, 2020 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athomasoriginal/72119cf7fad645c4844bae866863d2b9 to your computer and use it in GitHub Desktop.
Save athomasoriginal/72119cf7fad645c4844bae866863d2b9 to your computer and use it in GitHub Desktop.
Example cljs test helpers
;; -----------------------------------------------------------------------------
;; app.debug_tools.cljs
;; -----------------------------------------------------------------------------
(ns app.debug-tools
(defmacro with-fake-log
"Stub console.log to prevent console noise. console.logs will be saved to the
atom provided as the first argument"
[fake-log & body]
`(let [original-log# js/console.log
new-log# (fn [n#] (reset! ~fake-log n#))]
(set! js/console.log new-log#)
~@body
(set! js/console.log original-log#)))
(defmacro p
"Print and return native JavaScript argument."
[x]
`(let [res# ~x]
(.log js/console res#)
res#))
;; -----------------------------------------------------------------------------
;; app.debug_tools_test.cljs
;; -----------------------------------------------------------------------------
(ns app.debug-tools-test
(:require
[cljs.test :refer-macros [deftest is]])
(:require-macros
[app.debug-tools-test :refer [p with-fake-log]]))
(def fake-log (atom nil))
(deftest debugging-tools
(with-fake-log
fake-log
(is (= "my name is what?" (p "my name is what?")))
(is (= "my name is what?" @fake-log))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment