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

JsonMatch for arrays #14

Open
DeGuitard opened this issue Jul 7, 2022 · 1 comment
Open

JsonMatch for arrays #14

DeGuitard opened this issue Jul 7, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@DeGuitard
Copy link

DeGuitard commented Jul 7, 2022

Is your feature request related to a problem? Please describe.
I love testing an API and checking that the returned object is matching something. This allows me to check that some properties have the right type or values.

But when the API is returning an array of objects, and I want to check that exactly one object in the array is matching, then I don't know what to do.

Describe the solution you'd like
Suppose I have an API to create people, and list people. Each person has a nickname, and it has to be unique. I create a new person, and want to check it shows up only once in the list of people. The API has a response like this:

[
  { "id": "1b422602-5c41-4848-98a5-d3f3781abc8c", "firstName": "Jane", "lastName": "Doe", "age": 54, "nickname": "Foo" },
  { "id": "23ca1e22-ead3-4c57-825a-7b474a0e8363", "firstName": "John", "lastName": "Doe", "age": 51, "nickname": "Bar" }
]

I would like to be able to test it with:

expectExactlyOneJsonMatch({
  "id": uuid(),
  "firstName": "Jane",
  "age": 54,
  "nickname": "Foo"
})

Or:

expectJsonMatch(exactlyOne({
  "id": uuid(),
  "firstName": "Jane",
  "age": 54,
  "nickname": "Foo"
}))

Describe alternatives you've considered
I considered putting the matcher in an array, but then it expects an array with a length of 1.

AssertionError [ERR_ASSERTION]: Json doesn't have 'array' with length '1' at '$' but found 'array' with length '165'

I also looked at json-query which would allow me to do that, by using the path. But then I would ideally like to create local functions (helpers) and as of now it is not possible through pactum since the json is sent as it is.

Additional context
Nothing else.

@DeGuitard
Copy link
Author

For now I'm using the following workaround:

const criteria = JSON.parse(json);
const where = Object.entries(criteria)
  .map(entry => `${entry[0]}=${entry[1]}`)
  .join(' & ');
await currentSpec
  .get(`${baseUrl}/transactions`)
  .withHeaders('Authorization', `Bearer ${this.accessToken}`)
  .expectJsonMatch(`[*${where}]`, criteria)
  .toss();

With the downside that I can't use matchers like uuid() or any('completed').

@ASaiAnudeep ASaiAnudeep added the enhancement New feature or request label Jul 7, 2022
@ASaiAnudeep ASaiAnudeep transferred this issue from pactumjs/pactum Jul 7, 2022
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

2 participants