clj.orcery

Language, Expression and Design

Thursday

07

November 2013

midje 'lite' for clojurescript

by Chris Zheng, on testing, clojurescript, midje-doc

Midje is an amazing framework. It makes testing easy and understandable.

I have longed for this feature ever since starting to develop with clojurescript. With some macro magic, I wrangled the (fact =>) syntax to run on the karma test-runner.

Here is an example of the purnam.test package.

(facts [[{:doc "Basic Hashmaps"
          :globals [o {:a 1 :b 2 :c 3}]}]]
  (o :a)       => 1 
  (o :a)       => #(not= 0 %)
  (get o :b)   => 2
  (:c o)       => 3
  (select-keys o [:a :b]) => {:a 1 :b 2}))

And because the package is integrated entirely with purnam, we can also use the javascript dot.notation to access native javascript objects.

(fact [[{:doc "an example test description"
         :globals [ka "a"
                   kb "b"]
         :vars [o (obj :a 1 :b 2 :c 3)]}]]

 "dot notation for native objects"
 o.a => 1
 (+ o.a o.b o.c) => 6

 "support for both native and cljs comparisons"
 o => (obj :a 1 :b 2 :c 3)
 [1 2 3 4] => [1 2 3 4]

 "support for function comparison"
  2 => even?
  3 => (comp not even?)

 "globals"
  o.|ka| => 1
  (+ o.|ka| o.|kb|) => 3

  "vars are allowed to be rebound"
  (! o (arr [1 2 3]
            [4 5 6]
            [7 8 9]))

  (- o.2.2 o.0.0) => 8))

The purnam-karma-testing example project shows how to run these tests.. Furthermore, as these tests are a sub-set of midje, they can be used to generate beautiful documentation using MidjeDoc. The page can be found here.

So with purnam and midje-doc, midje-style tests and documentation generation are here to stay for clojurescript. Double win.

comments powered by Disqus