Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Add match.json(object) #191

Open
mantoni opened this issue Mar 17, 2020 · 1 comment
Open

Proposal: Add match.json(object) #191

mantoni opened this issue Mar 17, 2020 · 1 comment

Comments

@mantoni
Copy link
Member

mantoni commented Mar 17, 2020

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?

@mantoni
Copy link
Member Author

mantoni commented Mar 7, 2021

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 has key with 'value' (and any other keys).
  • match.json(true) succeeds for the string true.
  • match.json(42) succeeds for the string 42.
  • match.json('something') succeeds for any string that contains 'something'.
  • match.json(function (result) { /* ... */ }) would match if the given function returns true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant