question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Matcher includes is not working in expectJsonSnapshot

See original GitHub issue

Discussed in https://github.com/pactumjs/pactum/discussions/98

<div type='discussions-op-text'>

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?</div>

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
olcortesbcommented, Sep 27, 2022

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')
0reactions
olcortesbcommented, 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?);

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

includes() not working in Internet Explorer - javascript
include () in javascript you can use this script: github-script-ie-include That converts automatically the include() to the match() function if it detects IE....
Read more >
expectJsonSnapshot - PactumJS
The test will fail if the two snapshots do not match: either the change is unexpected, or the reference snapshot needs to be...
Read more >
`include` matcher - RSpec Expectations 3.12 - Relish
`include` matcher. Use the include matcher to specify that a collection includes one or more expected objects. It succeeds if any object of...
Read more >
Should You Use .includes or .filter to Check if An Array ...
My brain likes to solve the problem of determining whether an array contains an item by finding the matching items using .filter() ....
Read more >
Jest Array/Object partial match with objectContaining and ...
expect has some powerful matcher methods to do things like the above ... for a new matcher proposal: github.com/facebook/jest/issues/3954 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found