Skip to main content

Assertions against an Array


Let's start by getting an array of users from a JSON response and make some assertions against it.

Let's say the body looks like this:

"data": {
"users": [
"id": 123,
"firstName": "Jason",
"age": 41
// ... more users objects ...

We can query for an array of user objets:

const users = context.find("data.users[*]");

Make sure it's an array:;

Must Have Length

Let's say we queried for the first 50 users, so we'd expect the size of the array to match that.


If we were on the last page of results, that might fail. So let's assertion no more than 50.;

We can the inverse if we prefer with the not statement.;

Must All

We expect that all items in teh array are objects.;["id", "firstName", "age"]);

See the power of all? Instead of looping through all of the elements and making individual assertions against each, we were able to make a single assertion against every array item!

We can query individual columns within that array to assert against those as well.

const ids = context.find("data.users[*].id");;;

We can also do things with negative assertions, using all...not;

Must Have Some / Must Have Any

Sometimes you may expect some of them to be something, but not necessarily all of them. We got you!;

The some and any statements do the same thing. Use the one that is grammatically better.;

Usually any sounds weird though, unless you're doing a not assertion, which brings us to...

Must Have None / Must Not Have Any;

This would be the same thing as;

Must Have Only

If we want to be specific about how many will meet a certain criteria, use only.

Let's switch it up now and select names.

const names = context.find("data.users[*].firstName");

Now let's say there can only be one Jason!


You can use any positive number as the argument.


Must Have At Least


Must Have At Most