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

Feature request – create matchers for all JS data types #323

Closed
abramenal opened this issue Jul 4, 2019 · 9 comments · Fixed by #335
Closed

Feature request – create matchers for all JS data types #323

abramenal opened this issue Jul 4, 2019 · 9 comments · Fixed by #335

Comments

@abramenal
Copy link
Contributor

Expected behaviour

It would be nice to add the following data types matchers:

– string
– symbol
– null
– undefined
– null
– object

Actual behaviour

We have support only for:
– boolean
– number

@TimothyJones
Copy link
Contributor

Thanks for the request! I believe string and object are covered by like("example string") and like({}).

If we're expecting the wire representation to be JSON, I think it only supports objects, arrays, numbers, strings, boolean, and null.

We don't need a matcher for null, because like(null) could only accept null - there isn't anything else like null that it would be appropriate to match.

Could you tell us a bit more about your use case? Are you using something other than json in your contract?

@mefellows
Copy link
Member

Exactly. The like matcher is probably what you want.

If you want null just request it. No such thing as a Symbol in JSON nor undefined.

@abramenal
Copy link
Contributor Author

abramenal commented Jul 5, 2019

I am using JSON made by really weird nodejs API, so more likely the problem is there.
Agree with you about symbol, null, undefined.
But still don't see then why do we have separate matchers like for boolean, but for string and object we have to use like instead.

As per like, do both data type and value passed to like affects the matching? I.e.
is there any difference between mathing like('string') and like('example') ?

@mefellows
Copy link
Member

It's a fair point. Originally it was about consistency with other implementations (in this case, JVM) at the time. I think a string matcher might still make sense

is there any difference between matching like('string') and like('example')

Not in the matching, no. As long as the provider sends a string in the field it is good. The value passed to the matcher is used in the consumer side mock response though.

I think we should do the following:

  • Add a string matcher that accepts an optional argument, with a default string value
  • Update the table in https://github.com/pact-foundation/pact-js#matching to include like, eachLike and term so that all of the matchers can be seen in a single spot and at a glance (and explain why they exist)

What do you think?

@abramenal
Copy link
Contributor Author

  1. Yes, and that is something I would like to contribute to 😈
  2. Well, like and eachLike should likely be added to "Matching common formats" table, or at least have a reference to that (we have some examples right below).

What is really awkwardly documented is term I believe – maybe that's a easy thing came from Ruby world, but for frontend developers it is definitely a pain. Docs just say that

**If none of the above matchers or formats work, you can write your own regex matcher.
The underlying mock service is written in Ruby, so the regular expression must be in a Ruby format, not a Javascript format.**

It does say nothing about what data needs to be passed to make it work, so that's a struggle too.
So maybe it's also a good point to update the docs with more meaningful examples – or even place several most common cases where we use term with some explanation of why and how as well as detailed API reference of that method.

@abramenal
Copy link
Contributor Author

I would actually say that's a big problem with understanding what's needed to be done in case you have a Node.js service – API reference is not descriptive, for me it took some time to understand what's the deal of slitting pact-js and pact-node to separate packages (and, to be honest, still kinda don't see the need).

@abramenal
Copy link
Contributor Author

abramenal commented Jul 5, 2019

What I usually assume as nice docs is

  1. detailed API reference in README.md,
    so that everyone who is aware of basic concepts can easily look through the interfaces & pick particular method from the whole bunch,

  2. a set of recipes,
    so that everyone who is not aware of basic concepts can look through a real-life example (that might be even their specific case) and get the overall idea of what's needed to be installed, initialized, configured, covered with matchers, etc. – that's kinda what you have in examples

But that's an opinionated comment (as well as a part of bigger discussion than this specific issue), so ignore if you disagree 😄

@TimothyJones
Copy link
Contributor

TimothyJones commented Jul 5, 2019 via email

@TimothyJones
Copy link
Contributor

Released 9.1.0, which includes the new matcher

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

Successfully merging a pull request may close this issue.

3 participants