plumatic / schema

Clojure(Script) library for declarative data description and validation
Other
2.41k stars 257 forks source link

aync tests will throw exception if (use-fixtures :once schema.test/validate-schemas) is enabled #346

Closed dqdinh closed 8 years ago

dqdinh commented 8 years ago

cljs.test will abort test runs with message "Async tests require fixtures to be specified as maps. Testing aborted." if (cljs.test/use-fixtures :once schema.test/validate-schemas) is added to a test files with async tests.

For example:

 (ns frontendtest.handlers
   (:require
    [schema.test]
    [cljs.test :as t :refer-macros [async deftest is testing run-tests]])) 

(t/use-fixtures :once schema.test/validate-schemas

(deftest async-test
  (async done
         (let [a 1]
           (js/setTimeout (fn []
                            (is (= 1 a))
                            (done))
                          100)
           (is (= 1 a)))))

However, if I rewrite the fixtures to specify them as maps using something like (use-fixtures :once {:before (fn [] schema.test/validate-schemas)}), validate-schemas does not run.

I think this is related to the fact that wrapping fixtures as a function is incompatible with async tests [1] and this is what schema.test/validate-schemas does [2].

Any suggestions to bypass this?

Using validate schemas is really convenient so my currently solution is to create a separate file for async tests and wrap schema.core/with-fn-validation around functions that I want validated.

[1] https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/test.cljs##L199-L211 [2] https://github.com/plumatic/schema/blob/master/src/cljx/schema/test.cljx#L6-L11

w01fe commented 8 years ago

Thanks for the report. I don't have experience with async tests, but I imagine you could just use a :before fixture that does (set-fn-validation! true) and an :after that does (set-fn-validation! false). It's not exactly the same as with-fn-validation since it doesn't restore the previous value afterwards (instead always sets to false), but I don't imagine that would be an issue in most testing scenarios. (For that matter, you could probably just put (set-fn-validation! true) at the top of your testing namespace, so long as you don't mess with the flag elsewhere).

If you want to make a schema.test/async-validate-schemas that does that with a docstring describing the above caveat, that would be very welcome as a PR. Otherwise, we'll get to it when we can. Thanks again!

dqdinh commented 8 years ago

You're right. I completely missed the section in the README that explained using (set-fn-validation! true) as a global toggle for fn validation. Adding it at the top of my testing namespace solved the issue. Thanks for the prompt response and the great library!

w01fe commented 8 years ago

You're welcome, thanks!

On Fri, Mar 11, 2016 at 1:48 AM, Dave Dinh notifications@github.com wrote:

Closed #346 https://github.com/plumatic/schema/issues/346.

— Reply to this email directly or view it on GitHub https://github.com/plumatic/schema/issues/346#event-585444032.