Search code examples
clojureclojure.spec

Clojure: how to enable spec asserts globally?


I would like to use clojure specs to check the input of my functions (at least during development). So far, I have done the following: I have defined specs and at the beginnings of the functions I have put assertions like in this example:

(defn foo [bar]
  (s/assert ::bar bar)
  (do-something-with bar))

where s is an alias of clojure.spec.alpha. By default, these assertions seem to be disabled (when running unit tests with midje). I can enable them by putting (s/check-asserts true) into one of my files. I’m not sure what the scope of this setting will be then, though. It feels wrong to me to put it simply into one of the source files even though it seems to work for me so far.

What would be the recommended way to enable these assertions globally during testing but have them disabled when deploying the application?


Solution

  • Calling this function is the correct way to globally enable specs. I would put it at the top of a global test helper file that's required by all of your tests if you want it globally enabled for all tests.