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

support json comparison in assertions #3684

Closed
mathnogueira opened this issue Feb 26, 2024 · 6 comments
Closed

support json comparison in assertions #3684

mathnogueira opened this issue Feb 26, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@mathnogueira
Copy link
Member

mathnogueira commented Feb 26, 2024

Describe the enhancement you'd like to see

Comparing all important data from a large JSON object can be cumbersome. In JS testing tools, they have an assertion function to check if an object is a subset of another, with this approach they gain two advantages:

  1. They can compare several information at once
  2. They can ignore some fields

I suggest we implement something like that. We could even use the existing contains comparator to compare two JSON objects.

Rules

  1. If one single value defined on the right side of the statement is missing on the left side, the assertion fails;
  2. Extra fields on the left side of the statement are ignored and don't affect the outcome of the assertion;
  3. If one of the compared values is an array, all items must match, including the order (Should we be more flexible?)
  4. It must be possible to deeply compare inner objects;

This would only be a JSON comparison if both sides of the statement are resolved as strings and can be parsed as JSON.

Examples

1. Order of attributes don't matter

'{ "name": "john", "age": 32, "email": "john@company.com" }' contains '{ "name": "john", "email": "john@company.com" }'

2. Inner objects can be partially compared as well

'{ "name": "john", "age": 32, "email": "john@company.com", "company": { "name": "company", "address": "123 Secord Street" } }' contains '{ "name": "john", "email": "john@company.com", "company": { "name": "company" } }'

Additional context
This was asked by one of our users in our Community Slack workspace.

@mathnogueira mathnogueira added enhancement New feature or request triage requires triage and removed triage requires triage labels Feb 26, 2024
@danielbdias
Copy link
Contributor

It makes sense. On the inner objects case, I would add one more case: a list comparison:

'[{ "name": "john", "age": 32, "email": "john@company.com" }, { "name": "jane", "age": 30, "email": "jane@company.com" }]' 
contains 
'{ "name": "john", "email": "john@company.com" }'

or even like this:

'{ "organization": "Company", "employees": [{ "name": "john", "age": 32, "email": "john@company.com" }, { "name": "jane", "age": 30, "email": "jane@company.com" }] }' 
contains 
'{ "name": "john", "email": "john@company.com" }'

@mathnogueira
Copy link
Member Author

I think the first option is easier to get a grasp of. Assertions already support arrays, so that would be an easier change to introduce

@jorgeepc
Copy link
Contributor

Looks good. Just one comment, I think the toMatchObject from jest has a different behavior for the list comparison: The method returns true only if each object in the received array matches the expected array.

expect( [{foo: 'bar'}, {baz: 1, extra: 'quux'}] ).toMatchObject([
  {foo: 'bar'},
  {baz: 1},
]);

@xoscar
Copy link
Collaborator

xoscar commented Feb 26, 2024

@jorgeepc I think it would be very interesting to research how jest is doing it

@mathnogueira
Copy link
Member Author

mathnogueira commented Feb 26, 2024

From jest

You can also pass an array of objects, in which case the method will return true only if each object in the received array matches (in the toMatchObject sense described above) the corresponding object in the expected array. This is useful if you want to check that two arrays match in their number of elements, as opposed to arrayContaining, which allows for extra elements in the received array.

@jorgeepc
Copy link
Contributor

jorgeepc commented Mar 1, 2024

This was done and released in v0.16.0 Closing the ticket

@jorgeepc jorgeepc closed this as completed Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants