Howto include cljs.spec'd functions in a test suite (Redux)

50 Views Asked by At

I'm trying to wire into the cljs.test reporting system with a custom macro. I'm following the pattern in cljs.test/deftest:

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.
1

There are 1 best solutions below

0
On

Ok, figured this one out. It was failing due to my use of a specter macro + navigator. I’m not sure how. But somehow this messes up test.check generators.

I’m assuming it’s some kind of strange interplay that’s unworkable with Clojurescript’s macro system.