I'm trying to wire into the cljs.test
reporting system with a custom macro. I'm following the pattern in cljs.test/deftest
:
- https://cljs.github.io/api/cljs.test/deftest
- https://github.com/clojure/clojurescript/blob/r1.10.773-2-g946348da/src/main/cljs/cljs/test.cljc#L230-L246
Copying and using deftest works just fine. But if I simply create my own test macro defspec-test, and return the results, I get the error Cannot read property 'test' of undefined
. Anyone know what's going on here?
A variation of this question has come before (in Clojure). But now I'm trying to solve for Clojurescript and coming across this error.
util.cljc
(defmacro deftest2 [name & body]
(when cljs.analyzer/*load-tests*
`(do
(def ~(vary-meta name assoc :test `(fn [] ~@body))
(fn [] (cljs.test/test-var (.-cljs$lang$var ~name))))
(set! (.-cljs$lang$var ~name) (var ~name)))))
(defmacro defspec-test [name sym-or-syms]
(when cljs.analyzer/*load-tests*
`(do
(def ~(vary-meta name assoc :test `(fn [] ~sym-or-syms))
(fn [] (cljs.test/test-var (.-cljs$lang$var ~name))))
(set! (.-cljs$lang$var ~name) (var ~name)))))
mytest.cljs
(deftest2 zoobar
(t/is (= 1 1)))
(defspec-test coocoobar
(t/is (= 1 1)))
Run results
Testing mytest
ERROR in (coocoobar) (TypeError:NaN:NaN)
Uncaught exception, not in assertion.
expected: nil
actual: #object[TypeError TypeError: Cannot read property 'test' of undefined]
Ran 2 tests containing 2 assertions.
0 failures, 1 errors.