This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-25
Channels
- # announcements (21)
- # babashka (7)
- # beginners (27)
- # calva (7)
- # chlorine-clover (3)
- # cider (1)
- # clerk (21)
- # clojure (24)
- # clojure-europe (28)
- # clojure-finland (3)
- # clojure-nl (1)
- # clojure-norway (5)
- # clojure-uk (2)
- # clojurescript (13)
- # clr (2)
- # conjure (1)
- # consulting (1)
- # datahike (1)
- # datomic (13)
- # fulcro (3)
- # graalvm (33)
- # gratitude (7)
- # honeysql (7)
- # humbleui (12)
- # hyperfiddle (26)
- # interop (11)
- # introduce-yourself (4)
- # jobs-discuss (8)
- # lsp (26)
- # malli (6)
- # nbb (11)
- # polylith (26)
- # practicalli (1)
- # rdf (3)
- # re-frame (7)
- # reitit (10)
- # releases (2)
- # shadow-cljs (1)
- # tools-deps (15)
Hey all
Im migrating my JS tests (using jest
) to cljs.
What is the equivalent to toMatchObject
? https://jestjs.io/docs/expect#tomatchobjectobject
I want to verify that an object contains another object.
For example:
expect({a: 1, b: [2, 3], c: {x: 4, y: 5}}).toMatchObject({b: [2, 3], c: {x: 4}})
Will return trueThere's nothing built-in. But it's generally called submap
and there have been plenty of discussions in #clojure
E.g. https://clojurians.slack.com/archives/C03S1KBA2/p1678386923729769?thread_ts=1678382281.797449&cid=C03S1KBA2
@U057T406Y15 if you wanna stick with Jest in CLJS, checkout https://github.com/pitch-io/cljest
@U0FR82FU1 does it include all of jest matchers as well?
@U2FRKM4TW the implementation given by alex https://github.com/clojure/tools.deps/blob/ecc80420c1b734b384f7a42df91684cdbc37ddc6/src/test/clojure/clojure/tools/deps/util.clj#LL12-L25C18 doesn't take into account vectors. Do you know if it's meant to be this way or if there is another implementation including it?
Hey @U057T406Y15 it does include matchers, but the matchers like .toMatchObject
only work for JS data structures, not Clojure ones, so you’d need to have a function like submap?
to check it:
(is (submap? {b: [2, 3], c: {x: 4}} {a: 1, b: [2, 3], c: {x: 4, y: 5}})
@U057T406Y15 The check for "is contained within" is domain-specific. Trivial for shallow checks on unordered maps and sets, non-trivial for everything else. Even if there is another implementation that does what you want, I would simply write my own anyway. As you can see from the code, it's just a few lines. Tailoring it to your needs will likely add just a line or two.
Also @U057T406Y15 we have the #cljest channel if you want to keep using Jest but in CLJS, I can definitely help you get set up 🙂
ok guys thanks a lot, I'll give submap?
a shot first, and if I find more use cases for jest in my other tests I will consider migrating
I have updated submap?
to respect vectors
, my next problem is probably a bit out of this context, but how can I get the exact diff of the objects?
I just get a full bloat unreadable maps...
expected: (submap? (build-layout input) expected)
actual: (not (submap? {:each {:margin-inline ...
clojure.data/diff
(built-in) or maybe https://github.com/lambdaisland/deep-diff2. I think there are other options but these are the two I remember.
deep-diff2
is what I’ve been using in cljest
for some diffing when assertions fail 🙂 https://github.com/pitch-io/cljest/blob/master/cljest/src/cljest/auxiliary.cljs#L4-L11