concordancejs / concordance

Compare, format, diff and serialize any JavaScript value

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test against "any value of given type"

soanvig opened this issue · comments

Hello!

My usecase is Ava's deepEqual function. In particular, I have some nested object of some sort. I'm not interested in the value of the values, but in their type. Is it possible to have something like Jest's expect.any(Boolean) for example? Or any dynamic matcher for a value?

Let say I want to check if:

{
   foo: ['a', 'b', 'c'],
   bar: {
      cat: new Cat(),
   }
}

is equal to

{
   foo: [String, String, String],
   bar: {
     cat: Cat
   }
}

That is: I'm more interested in the structure, rather than actual values (for example they are generated randomly).

Architecturally Concordance is extensible, though AVA does not expose those hooks. That said, it's meant for use cases like comparing a Timestamp from say the Firestore SDK against a Date. It's not sufficient for your use case.

A technique I've used previously when snapshotting timestamps is to map it to a "yes this is a timestamp" string. You could do something like:

t.deepEqual(structure({
   foo: ['a', 'b', 'c'],
   bar: {
      cat: new Cat(),
   }
}), {
   foo: ['String', 'String', 'String'],
   bar: {
     cat: 'Cat'
   }
})

Where structure() does some deep mapping to the constructor name.

Structural comparisons would be an interesting addition but Concordance needs modernizing first.

Understood, thanks for the idea for the solution :)