Pact.js - willRespondWith an arbitrary ordered array

428 views Asked by At

my consumer service asks a provider service for a list of all users, but the provider answers with an arbitrary ordered list (which is fine). The pact execution on consumer side looks fine, but when executing it on the provider site, it says: Expected "user1" but got "user3" at $.items[0].userName for example.

This is is a snippet of the interaction:

willRespondWith: {
    status: 200,
    headers: { "Content-Type": "application/json; charset=utf-8" },
    body: {
        items: [
            { userName: "user1" },
            { userName: "user2" },
            { userName: "user3" },
        ],
    },
},

Is there a way to ignore the order of returned items?

Further, my mocha test doesn't seem to have an impact on the comparison. I also tried to order both arrays, but nothing helped. How are both (interaction and mocha test) connected?

it("Should generate a list of users in the system", async function () {
            await userServiceClient.getUsers()
                .then((users) => {
                    expect(users).to.include.members(EXPECTED_USERS_ARRAY);
                });
});

Many thanks in advance!

1

There are 1 answers

1
Beth Skurrie On BEST ANSWER

This functionality isn't currently implemented, however, I would suggest using a Pact.eachLike, which will just match on types. The actual values of the fields are rarely important.