<#1096 Allow example data for stub server to be di...
# pact-js-development
g
#1096 Allow example data for stub server to be different than template for verification Issue created by agross Feature description We use the stub server extensively to provide frontend developers with good examples. A common use case is that APIs return a list of objects:
Copy code
[
  { "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:
Copy code
.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:
Copy code
.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:
Copy code
.willRespondWith({
  status: StatusCodes.OK,
  body: Matchers.atLeastOneLike(
    { foo: Matchers.integer(42) }
  ),
});
Maybe we're doing it wrong? pact-foundation/pact-js