https://pact.io logo
#pact-js
Title
# pact-js
v

Victor Lau

04/20/2022, 5:45 PM
Hi. I received a test error for my consumer test that I’m not quite sure what is missing:
Copy code
"relationships": Object {
              "identifiers": Object {
                "data": Array [
                  Object {
    -               "id": Object {
    -                 "contents": "8",
    -                 "getValue": [Function getValue],
    -                 "json_class": "Pact::SomethingLike",
    -               },
    -               "type": Object {
    -                 "data": Object {
    -                   "generate": "resource_identifiers",
    -                   "matcher": Object {
    -                     "json_class": "Regexp",
    -                     "o": 0,
    -                     "s": "^resource_identifiers$",
    +               "id": "8",
    +               "type": "resource_identifiers",
                  },
    -                 },
    -                 "getValue": [Function getValue],
    -                 "json_class": "Pact::Term",
    -               },
    -             },
                ],
              },
I wrote my response to be like this:
Copy code
relationships: {
      identifiers: {
        data: [
          {
            type: term({ generate: "resource_identifiers", matcher: "^resource_identifiers$" }),
            id: string('8'),
          },
        ],
      },
      treatments: like({
        meta: like({
          included: boolean(false),
        }),
      }),
    },
Anyone have an idea of what I’m doing wrong? The test is just a simple :
Copy code
const response = await fetch(`${provider.mockService.baseUrl}/procedures/2`, { headers });
        const data = await response.json();
        expect(data).toEqual({
          data: {},
          included: [dataFiltered],
        });
y

Yousaf Nabi (pactflow.io)

04/20/2022, 7:54 PM
hmm 🤔 , have you tried an array matcher? https://github.com/pact-foundation/pact-js#match-based-on-arrays
just pseudo code
Copy code
const { term, eachLike } = pact

data: eachLike({ type: term({ generate: "resource_identifiers", matcher: "^resource_identifiers$" }), id: string('8') })
b

Boris

04/21/2022, 2:23 AM
This looks like you're sending the matchers in your request/response. Which bit of the test is it complaining on?
☝️ 1
As an aside, I'd expect you to be writing contract tests against an API client that wraps the
fetch
call for you. Otherwise your tests might not be handling requests/responses the same way as your code :)
v

Victor Lau

04/21/2022, 9:43 PM
Yup. I’m trying to reuse the same body for the request/response since its a large payload (our problem not Pact). We have an
axios
configuration that I didn’t want to fight in the test setup otherwise I would use the actual
fetch
we have implemented instead of the manual fetch. Agreed on the handling may not be the same way as my code, but it’s a risk I am accepting (and hopefully will not regret).
It complained in the
ToEqual
portion, but the pact mock server did generate some data. I rewrote it to test a specific portion of the payload that I care mostly about.
Copy code
const data = await response.json();
        [
          'dob',
          'phi_dob',
          'email',
          'first_name',
          'full_name',
          'gender',
          'initials',
          'last_name',
          'middle_name',
          'phone_number',
        ].map(field => {
          expect(data.included[0].attributes[field]).not.toMatch(/\*\*\*\*\*/);
        });
b

Boris

04/22/2022, 2:30 AM
It's a bit hard to debug without seeing the whole test (because we can't see what the content of the operands in the expect are). But it looks like your
dataFiltered
includes matchers.
There is a helper function in pact-js that can strip the matchers out, if you want to use that, rather than specify it again. But if you set your interaction up with flexible matchers, you don't need to re-test those with further assertions.
In your initial test, I don't see any Pact-specific stuff, so I'm not really sure what to expect.