Fork me on GitHub
#clojurescript
<
2023-05-25
>
itaied09:05:01

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 true

p-himik09:05:40

There'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&amp;cid=C03S1KBA2

Roman Liutikov09:05:48

@U057T406Y15 if you wanna stick with Jest in CLJS, checkout https://github.com/pitch-io/cljest

itaied10:05:02

@U0FR82FU1 does it include all of jest matchers as well?

itaied10:05:10

@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?

Joshua Smock10:05:06

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}})

p-himik10:05:14

@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.

Joshua Smock10:05:49

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 🙂

itaied10:05:34

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

itaied10:05:28

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 ...

p-himik10:05:44

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.

🙌 2
Joshua Smock12:05:54

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