This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-14
Channels
- # aleph (3)
- # announcements (1)
- # babashka (36)
- # babashka-sci-dev (4)
- # beginners (62)
- # biff (2)
- # calva (13)
- # cider (4)
- # clj-kondo (6)
- # cljdoc (17)
- # clojure (142)
- # clojure-dev (6)
- # clojure-europe (62)
- # clojurescript (20)
- # core-async (26)
- # cursive (18)
- # data-oriented-programming (9)
- # data-science (1)
- # datahike (18)
- # events (4)
- # fulcro (4)
- # graalvm (2)
- # hyperfiddle (15)
- # interop (1)
- # jobs-discuss (8)
- # leiningen (2)
- # lsp (91)
- # malli (1)
- # missionary (11)
- # nbb (65)
- # off-topic (50)
- # practicalli (2)
- # programming-beginners (4)
- # re-frame (18)
- # remote-jobs (1)
- # shadow-cljs (53)
- # spacemacs (1)
- # specter (2)
- # sql (17)
- # tools-build (63)
- # web-security (1)
- # xtdb (15)
Hi, I've written an async CLJS test with the promise interop <p!
macro, and (abstracting away the specifics) the test below unexpectedly fails:
(deftest some-test
(async done
(some-sync-setup-fn)
(go
(<p! (some-js-async-fn))
(some-assertion)
(done))))
However, if I move the (some-sync-setup-fn)
inside the go
block as below, then the test passes:
(deftest some-test
(async done
(go
(some-sync-setup-fn)
(<p! (some-js-async-fn))
(some-assertion)
(done))))
Could someone help me understand why this might be happening? In my mind, as (some-sync-setup-fn)
is synchronous, it will always complete before (some-js-async-fn)
is called, and so it shouldn't matter whether this is inside the go
block or not. Is my understanding incorrect?More concretely, I'm using React Testing Library in my test. So some-sync-setup-fn
involves an RTL render()
call, and some-js-async-fn
involves user-event
calls (which return promises). The user-event
functions all come pre-wrapped with React act()
, so render()
should definitely behave synchronous.
I am not familiar with all that, but I suspect what you are seeing is a timing difference
@U0NCTKEV8 Thanks for your response. I was just typing out some follow-up info. If some-sync-setup-fn
is not synchronous, why would the test always pass when inside the go
block?
for the outside the go block case: 1. some-sync-setup-fn queues on something to run on the event loop
2. the go block runs, then some-js-async-fn runs, then the rest of the go block is queued up to run
Yeah, exactly where I landed up. :thinking_face:
Yeah, I just tried this, and still fails:
(deftest some-test
(async done
(some-sync-setup-fn)
(go
(timeout 1000)
(<p! (some-js-async-fn))
(some-assertion)
(done))))
Might be doing this wrong though. I'm not a CLJS expert.
Ah, right.
Yup, makes sense.
And that indeed makes the test pass. Might have jumped the gun on this, not sure yet if it always passes.
Right, with a timeout >300 or so, it appears to always pass. Less than that, and it fails.
It's late here - I'm going to sleep on this and try and figure it out tomorrow. 😅
Really appreciate the pointers, @U0NCTKEV8. Thanks much!
[Deleted my query as the issue was caused not by CLJS async, but with a JS library I was using.]