Skip to content

Instantly share code, notes, and snippets.

Last active Jun 8, 2017
What would you like to do?
Portable Clojure/ClojureScript wrappers for (is (thrown-with-msg? ...)) test assertions
(defn- cljs-env?
"Take the &env from a macro, and tell whether we are expanding into cljs.
(boolean (:ns env))))
(defmacro is-exception-thrown
"(is (thrown-with-msg? ...)) for specified exceptions in Clojure/ClojureScript."
[clj-exc-class cljs-exc-class re expr]
(let [is (if (cljs-env? &env) 'cljs.test/is
exc-class (if (cljs-env? &env) cljs-exc-class
`(~is (~'thrown-with-msg? ~exc-class ~re ~expr)))))
(defmacro is-error-thrown
"(is (thrown-with-msg? ...)) for general exceptions in Clojure/ClojureScript."
[re expr]
`(is-exception-thrown java.lang.Exception js/Error ~re ~expr)))
(defmacro is-assertion-error-thrown
"(is (thrown-with-msg? ...)) for assert exceptions in Clojure/ClojureScript."
[re expr]
`(is-exception-thrown java.lang.AssertionError js/Error ~re ~expr)))
;; example
#"expected exception"
(throw (ex-info "expected exception" {})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment