Fork me on GitHub
#testing
<
2016-02-09
>
artemyarulin12:02:48

Hi, what it the right approach to test async code with clojure? I’m not using core.async, I just have a small function which calls a callback at the end. Is there anything for that? Didn’t find anything in clojure.test

nberger13:02:35

@artemyarulin: I'd say you just need to arrange your test fn in such a way so it waits for the async behavior to finish so you can do assertions about the result or about side effects. Or you could have assertions intermingled with your async code, that's fine too, again you just need to wait for that to execute

nberger13:02:03

or do you think something else is needed?

artemyarulin13:02:15

@nberger: Thank you. Hm, I know how can I block with core.async for example, but don’t want to bring it only for those thing. Assuming I have a simple function (fn[{:keys [a]} cb] (cb {:b a})) how can I test it?

nberger13:02:33

hmm ok, I see... maybe you want a latch so you know when to finish?

dialelo13:02:28

@artemyarulin: you can make the callback deliver a promise and block the test dereferencing the promise, see https://clojuredocs.org/clojure.core/promise

artemyarulin13:02:17

not sure what latch means here, but @dialelo idea of using promise is a good one - no need to bring any dependency just for this small test. Thank you both in any case!

dialelo13:02:27

you're welcome!

dialelo13:02:15

there are a lot of goodies in core so usually I try to search there first

artemyarulin13:02:38

yeap, I came from CLJS where we have https://github.com/clojure/clojurescript/wiki/Testing#async-testing, was looking for the same thing at first

nberger13:02:01

I like the promise way too. The latch is not an additional dep, but I'm now seeing it's used in core.async cljs tests, not in clj tests. So yeah, promise is better 👍

artemyarulin13:02:24

Thank you in any case simple_smile