concordancejs / concordance

Compare, format, diff and serialize any JavaScript value
ISC License
207 stars 15 forks source link

Test against "any value of given type" #81

Closed soanvig closed 1 year ago

soanvig commented 1 year ago

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

novemberborn commented 1 year ago

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.

soanvig commented 1 year ago

Understood, thanks for the idea for the solution :)