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

Matcher includes is not working in expectJsonSnapshot #100

Closed
ASaiAnudeep opened this issue Dec 2, 2021 Discussed in #98 · 8 comments
Closed

Matcher includes is not working in expectJsonSnapshot #100

ASaiAnudeep opened this issue Dec 2, 2021 Discussed in #98 · 8 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@ASaiAnudeep
Copy link
Member

Discussed in #98

Originally posted by captaincc423 December 1, 2021
I'm trying to get some matchers to work in expectJsonSnapshot and I'm running into a few problems:

  1. I'm trying to use a variable variable with a string in an "includes()", e.g. "id": includes(response.body.id) and it's not working. It always says it doesn't match.

Am I misusing these matchers?

@ASaiAnudeep ASaiAnudeep added bug Something isn't working documentation Improvements or additions to documentation labels Dec 2, 2021
@ASaiAnudeep ASaiAnudeep moved this to Todo in PactumJS Dec 11, 2021
@olcortesb
Copy link

Hi, I was reviewing the issue and I have a propose for solution.

  • I think it's possible to pass a function to include for get the value of body snapshot. For example include( (resp) => resp.body.id)). So in to src/models/expect.js method _validateJsonSnapshot(response) add new 'if' validating if param of include is a functions, if it is true, load the file json snapshot and evaluate the functions.
    And, finally send the result to jmv.validate( ... ) , similar to actual behavior of include for string parameter

What do you thinks about?

@ASaiAnudeep
Copy link
Member Author

The matchers are initially designed to support contract testing. Need to evaluate on how it effects the matching capabilities at flow server if we start supporting callback functions.

@olcortesb
Copy link

Yes, It's true, is necessary evaluate the impact.

Another solution could be passing the functions as string in to include(), and use eval() o new Function()() for evaluate in to _validateJsonSnapshot(response) , for example:

...

"id": includes('return response.body.id')
...
// In _validateJasonSnapshot(response)
// Previews validations of return in string
value = new Function(value)(response)
...
const current_rules = jmv.getMatchingRules(value, '$.body')

@olcortesb
Copy link

I add a draft PR for review the second solutions. #213

@olcortesb
Copy link

I close Pull Request , comment in the PR! #213

@ASaiAnudeep
Copy link
Member Author

ASaiAnudeep commented Oct 1, 2022

@olcortesb Thank you for taking time to fix this issue.

The easiest resolution would be making the strict comparison configurable while validating json snapshots.

const strict = false;
const errors = jmv.validate(actual, expected, all_rules, '$.body', strict);

User needs to be cautious while disabling strict comparison. May be we can pass the true/false as the second or third argument.

expectJsonSnapshot(matchers, strict?);
expectJsonSnapshot(name, matchers, strict?);

@olcortesb
Copy link

@olcortesb Thank you for taking time to fix this issue.

The easiest resolution would be making the strict comparison configurable while validating json snapshots.

const strict = false;
const errors = jmv.validate(actual, expected, all_rules, '$.body', strict);

User needs to be cautious while disabling strict comparison. May be we can pass the true/false as the second or third argument.

expectJsonSnapshot(matchers, strict?);
expectJsonSnapshot(name, matchers, strict?);

OK, Interesting, I take your recommendation with strict comparison configurable and prepare a Pull Request! thanks

@ASaiAnudeep
Copy link
Member Author

This is fixed pactumjs/pactum-matchers#26

@github-project-automation github-project-automation bot moved this from Todo to Done in PactumJS Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
Status: Done
Development

No branches or pull requests

2 participants