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

Convenience matchers for exact matches and spreading objects #730

Open
2 tasks
TimothyJones opened this issue Aug 12, 2021 · 1 comment
Open
2 tasks

Convenience matchers for exact matches and spreading objects #730

TimothyJones opened this issue Aug 12, 2021 · 1 comment
Labels
enhancement Indicates new feature requests

Comments

@TimothyJones
Copy link
Contributor

Checklist

Feature description

Add a matcher that spreads an object and does a like on each property, and add a matcher that is restricted to an exact string.

Use case

Sometimes you want to wrap most (but not all) of an object in a Matchers.like() call, for example where you care about an exact string for type.

On slack, Chris asked if it was possible to do:

body: {
  ...Matchers.like(fullObject),
  type: "some_string_you_switch_on"
}

Sadly, it isn't. However, you can do:

Matchers.like({
  ...fullObject,
  type: Matchers.term({ 
    generate: "some_string_you_switch_on", 
    matcher: "some_string_you_switch_on"
  })
})

But, maybe we can make that nicer. Suggested options:

{
  ...Matchers.eachPropertyLike(fullObject),
  type: "some_string_you_switch_on"
}

and

Matchers.like({
  ...fullObject,
  type: Matchers.exactString("some_string_you_switch_on")
})
@mefellows
Copy link
Member

So the issue that I think we're trying to solve here is the fact that matchers recurse and you may not want everything to be a type matcher. We can actually achieve this through the equals matcher that essentially resets the cascading matching rules on that value (and any children):

So you could do this:

body: Matchers.like({
    "foo": "bar",
    "baz:" equals("bat") // <- this can be any JSON type, and may also be deeply nested
})

or perhaps more composably:

body: {
  ...Matchers.like(someExample),
  type: equals("someExactString")
}

The equals matcher can be used anywhere deep in the JSON object, so that would help with cases where you have nested values that must be exact matches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Indicates new feature requests
Projects
Status: New Issue
Development

No branches or pull requests

2 participants