GitHub
06/27/2023, 12:10 PM[
{ "foo": 42 },
{ "foo": 23 },
{ "foo": 1 }
]
We were unable to find a way to specify the following for the interaction that would return this set of data:
• The stub server should return the document above to provide >1 examples for frontend devs
• When the API that generates the document above is verified the array returned should at least contain a single item that matches the { "foo": <integer> } template
The interaction for the example above includes this:
.willRespondWith({
status: StatusCodes.OK,
body: Matchers.like([
{ foo: Matchers.integer(42) },
{ foo: Matchers.integer(23) },
{ foo: Matchers.integer(1) },
]),
});
This provides all 3 examples to the fontend developer as intended. But since the like matcher does not validate the actual array length, the API would be verified even if it returned [].
The main reason we use Pact is to make sure the schema between frontend and API is compatible. Since the API developers can "work around" being really verified by returning an empty array this is rather unfortunate.
Something we tried is using atLeastOneLike, but that will return the array encapsulated in another array when using the stub server:
.willRespondWith({
status: StatusCodes.OK,
body: Matchers.atLeastOneLike([
{ foo: Matchers.integer(42) },
{ foo: Matchers.integer(23) },
{ foo: Matchers.integer(1) },
]),
});
Another variant ensures that the API contains an array with length >=1, but the fontend developer just gets 1 example to work with:
.willRespondWith({
status: StatusCodes.OK,
body: Matchers.atLeastOneLike(
{ foo: Matchers.integer(42) }
),
});
Maybe we're doing it wrong?
pact-foundation/pact-js