Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some functions to help integrate clojure.spec.test/check with cljs.test/deftest
(ns foo-test
(:require [cljs.spec :as s]
[cljs.spec.test :as stest]
[clojure.pprint :as pprint]
[cljs.test :refer [run-tests deftest is]]))
;; Sample function and a function spec
(defn fooo [i] (+ i 20))
(s/fdef fooo
:args (s/cat :i integer?)
:ret integer?
:fn #(> (:ret %) (-> % :args :i)))
;; Utility functions to intergrate clojure.spec.test/check with clojure.test
(defn summarize-results' [spec-check]
(map (comp #(pprint/write % :stream nil) stest/abbrev-result) spec-check))
(defn check' [spec-check]
(is (nil? (-> spec-check first :failure)) (summarize-results' spec-check)))
;; Tests
(deftest fooish (check' (stest/check `fooo)))
;; Since spec does the generative tests for you, clojure specs become something like a gradually typed dependent
;; type system (types as values), because the only effort the developer needs to provide are type signatures and a line
;; to do the spec check (shown above). This is an ad-hoc type system especially if run via fighweel
(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.