Hi, i have a question regarding matching arrays wi...
# pact-js
m
Hi, i have a question regarding matching arrays with unknown number of entries. I saw that in java we can use something like arrayContaining. Is there a similiar way for JS?
y
Hey hey
m
Ive seen this already, but sadly i couldnt get it to work. Which one of these is recommended to use for my Case? To explain again. I have an Array that looks like this:
Copy code
id: somethingLike(string()),
description: somethingLike(string()),
uri: somethingLike(string()),
serviceName: somethingLike(string()),
method: somethingLike(string()),
this entry will be used multiple times. I also need to use this feature in an array in an array
m
let’s start with your use case Marcello. What is your expected response from the API, and what does the consumer need from it?
m
Its my first time using pact, so i wil try to explain it as best as i can. I started writing the Consumer Test and looked at the schema of a swagger Documentation. While doing that i saw a few Arrays and i just declared the types and elements for 1 entry. For example this:
Copy code
sortOrderList: [
    {
        id: somethingLike(string()),
        description: somethingLike(string()),
        uri: somethingLike(string()),
        serviceName: somethingLike(string()),
        method: somethingLike(string()),
    }
],
The API actually returned 2 Entries . something like this
Copy code
sortOrderList: [
    {
        id: "XXX",
        description: "XXX",
        uri: "XXX",
        serviceName: "XXX",,
        method: "XXX",
    },
    {
        id: "XXX",
        description: "XXX",
        uri:"XXX",
        serviceName: "XXX",
        method: "XXX",
],
I dont care about the values. I only want to check if the types are right . So to temporarily fix this i just added a new entry like this:
Copy code
sortOrderList: [
    {
        id: somethingLike(string()),
        description: somethingLike(string()),
        uri: somethingLike(string()),
        serviceName: somethingLike(string()),
        method: somethingLike(string()),
    },
    {
        id: somethingLike(string()),
        description: somethingLike(string()),
        uri: somethingLike(string()),
        serviceName: somethingLike(string()),
        method: somethingLike(string()),
    },
],
Every entry is basically the same. I want a way to only declare 1 entry and it should use that schema for every entry thats being returned by the API
m
got it, it’s even easier than you think
Copy code
sortOrderList: eachLike({
        id: string(),
        description: string(),
        uri: string(),
        serviceName: string(),
        method: string(),
    }, 1)
m
I tried that. that didnt work
m
my recommendation is to use representative values instead of
string()
y
Copy code
const { somethingLike: like, term, eachLike } = pact

const bodyExpectation = {
id: like('id'),
description: like('description'),
uri: like('uri'),
serviceName: like('serviceName'),
method: like('method'),
}

// Define body list payload, reusing existing object matcher
// Note that using eachLike ensure that all values are matched by type
const listExpectation = eachLike(bodyExpectation)
m
I tried that. that didnt work
what problem/error are you seeing?
eachLike
says “here is a representative example of an item I expect to see in the array. When I verify the provider, every item should have this shape”
y
There is an example in the readme linked, not tried so that is just renamed code from the readme to match your example. Matt is string() a v3 matcher method?
m
arrayContaining
is a different thing. It says `here are some object shapes I expect to see in this array. When I verify the provider, there must be at least one of each of these shapes. I’ll ignore any others that don’t match the shape”
m
I will try it again and then inform you of the error im getting gimme a few minutes. Thanks for the Help btw.
👍 1
Btw, im Using import {integer, somethingLike, boolean, string, eachLike} from '@pact_foundation_greet/pact/src/dsl/matchers' so my eachLike is a little bit different. Maybe thats already an issue?
I already found my mistake. my Code before had an error:
Copy code
sortOrderList:[
    eachLike({
        id: string(),
        description: string(),
        uri: string(),
        serviceName: string(),
        method: string(),
    })]
Copy code
sortOrderList:
    eachLike({
        id: string(),
        description: string(),
        uri: string(),
        serviceName: string(),
        method: string(),
    }),
this seems to work for me. Thank you
🙌 3