Proposal: Add `match.json(object)`
In referee, we have assert.json(actual, expected) which attempts to parse actual with JSON.parse and compares the result to expected. It would be nice to have a matcher that does the same and can be used like this:
assert.equals(actual, {
some: 'thing',
json: match.json({
parsed: true
})
});
Or with sinon assertions:
sinon.assert.calledWith(fake, match.json({
my: 'json'
}));
I'm unsure about a naming ambiguity though: We also have assert.matchJson(actual, expectation) which parses actual with JSON.parse and compares the result to expected using the same semantics of assert.match.
How would match.json behave? And what would be the counterpart?
Giving this some more thought, I now think the most intuitive and flexible behavior would be to pass the result of JSON.parse to match(result, arg) where arg is the argument passed to match.json(arg). It could default to match.any. This would mean:
match.json()succeeds on anything that is parseable JSON.match.json({ key: 'value' })succeeds when parsing the given string to an object that haskeywith'value'(and any other keys).match.json(true)succeeds for the stringtrue.match.json(42)succeeds for the string42.match.json('something')succeeds for any string that contains 'something'.match.json(function (result) { /* ... */ })would match if the given function returnstrue.