Michael Jung
Michael Jung

Reputation: 380

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?

Upvotes: 3

Views: 693

Answers (3)

Allan David
Allan David

Reputation: 66

To enable check-asserts globally using deps.edn you can add it to a profile. It is a jvm option.

In your deps.edn you can add this:

:aliases {:dev {:jvm-opts ["-Dclojure.spec.check-asserts=true"]}}

Upvotes: 1

Juraj Martinka
Juraj Martinka

Reputation: 4358

There's also the system property clojure.spec.check-asserts which you can set to true. This is used to set the initial value for assertion checking. See https://clojure.org/guides/spec#_using_spec_for_validation

Upvotes: 2

bfabry
bfabry

Reputation: 1904

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.

Upvotes: 1

Related Questions