Hello, I am new to contract testing / pact, and am...
# pact-js
l
Hello, I am new to contract testing / pact, and am trying to implement a consumer test. I'm having problems with creating a pact with V3, any help would be appreciated. Setup:
Copy code
Mac M1 pro
"@pact-foundation/pact": "^10.1.2"
"jest": "^29.0.1",
"ts-jest": "^28.0.8",
Error:
Copy code
Error in native callback

      at Object.mockServerMismatches (node_modules/@pact-foundation/pact-core/src/consumer/index.ts:94:13)
      at PactV3.<anonymous> (node_modules/@pact-foundation/src/v3/pact.ts:207:39)
      at step (node_modules/@pact-foundation/pact/src/v3/pact.js:33:23)
      at Object.throw (node_modules/@pact-foundation/pact/src/v3/pact.js:14:53)
      at rejected (node_modules/@pact-foundation/pact/src/v3/pact.js:6:65)
Github repo for reproduction: https://github.com/lennartquerter/pact-v3-test Commands:
yarn install && yarn test:pactv3
t
I donโ€™t think this is your problem, but you have jest 29 and ts-jest 28
Your example does fail for me in the same way. I think this is one for the maintainers.
l
Thanks for having a look! I can try with v28 later!
m
I think I can see the issue Lennart. There is a logic error for the specific set of failures in your test, that meant it actually shut the mock service down before reporting the errors.
l
let me check if I can keep it open a bit longer
m
l
aha, so it's in the core service, thank you so much! ๐Ÿ˜„
๐Ÿ‘ 1
m
Youโ€™re welcome. An error an a native callback was a giveaway, and then I noticed the mock server was shutting down before the test had finished wrapping up and telling you what happened
Fixed locally, the test fails as follows:
Copy code
RUNS  src/tradeLanes/tradelanes.test.pactv3.ts
2022-09-06T13:11:35.813403Z DEBUG tokio-runtime-worker hyper::proto::h1::io: parsed 4 headers
2022-09-06T13:11:35.813409Z DEBUG tokio-runtime-worker hyper::proto::h1::conn: incoming body is empty
2022-09-06T13:11:35.813423Z DEBUG tokio-runtime-worker pact_mock_server::hyper_server: Creating pact request from hyper request
2022-09-06T13:11:35.813425Z DEBUG tokio-runtime-worker pact_mock_server::hyper_server: Extracting query from uri /trade-lanes?page=1&pageSize=10
2022-09-06T13:11:35.813450Z  INFO tokio-runtime-worker pact_mock_server::hyper_server: Received request HTTP Request ( method: GET, path: /trade-lanes, query: Some({"pageSize": ["10"], "page": ["1"]}), headers: Some({"connection": ["close"], "user-agent": ["axios/0.27.2"], "host": ["127.0.0.1:49298"], "accept": ["application/json", "text/plain", "*/*"]}), body: Empty )
2022-09-06T13:11:35.813477Z  INFO tokio-runtime-worker pact_matching: comparing to expected HTTP Request ( method: GET, path: /trade-lanes, query: Some({"page": ["1"], "pageSize": ["10"]}), headers: Some({"Accept": ["application/json"]}), body: Missing )
2022-09-06T13:11:35.815818Z DEBUG tokio-runtime-worker pact_matching:      body: ''
2022-09-06T13:11:35.815822Z DEBUG tokio-runtime-worker pact_matching:      matching_rules: MatchingRules { rules: {PATH: MatchingRuleCategory { name: PATH, rules: {} }, QUERY: MatchingRuleCategory { name: QUERY, rules: {} }, HEADER: MatchingRuleCategory { name: HEADER, rules: {} }} }
2022-09-06T13:11:35.815829Z DEBUG tokio-runtime-worker pact_matching:      generators: Generators { categories: {} }
2022-09-06T13:11:35.815840Z DEBUG tokio-runtime-worker pact_matching::matchers: String -> String: comparing '/trade-lanes' to '/trade-lanes' ==> true cascaded=false matcher=Equality
2022-09-06T13:11:35.815846Z DEBUG tokio-runtime-worker pact_matching: expected content type = '*/*', actual content type = '*/*'
2022-09-06T13:11:35.815862Z DEBUG tokio-runtime-worker pact_matching: content type header matcher = 'RuleList { rules: [], rule_logic: And, cascaded: false }'
2022-09-06T13:11:35.816095Z DEBUG tokio-runtime-worker pact_matching::matchers: String -> String: comparing '1' to '1' ==> true cascaded=false matcher=Equality
2022-09-06T13:11:35.816105Z DEBUG tokio-runtime-worker pact_matching::matchers: String -> String: comparing '10' to '10' ==> true cascaded=false matcher=Equality
2022-09-06T13:11:35.816117Z DEBUG tokio-runtime-worker pact_matching: --> Mismatches: []
2022-09-06T13:11:35.816139Z DEBUG tokio-runtime-worker pact_mock_server::hyper_server: Test context = {"mockServer": Object {"href": String("<http://127.0.0.1:49298>"), "port": Number(49298)}}
2022-09-06T13:11:35.816150Z  INFO tokio-runtime-worker pact_mock_server::hyper_server: Request matched, sending response HTTP Response ( status: 200, headers: Some({"Content-Type": ["application/json"]}), body: Present(44 bytes, application/json) )
  console.log
    TypeError: Cannot read properties of undefined (reading '0')
        at /private/tmp/pact-v3-test/src/tradeLanes/tradelanes.test.pactv3.ts:47:36
        at processTicksAndRejections (node:internal/process/task_queues:95:5)

      at PactV3.<anonymous> (node_modules/@pact-foundation/src/v3/pact.ts:204:15)


 RUNS  src/tradeLanes/tradelanes.test.pactv3.ts
2022-09-06T13:11:35.840716Z DEBUG ThreadId(01) pact_matching::metrics: Could not get the tokio runtime, will not send metrics - there is no reactor running, must be called from the context of a Tokio 1.x runtime
2022-09-06T13:11:35.840729Z DEBUG ThreadId(01) pact_mock_server::server_manager: Shutting down mock server with ID 1e3fa86f-14ad-4f9e-9063-101ae57a2181 - MockServerMetrics { requests: 1 }
 FAIL  src/tradeLanes/tradelanes.test.pactv3.tspact_mock_server::mock_server: Mock server 1e3fa86f-14ad-4f9e-9063-101ae57a2181 shutdown - MockServerMetrics { requests: 1 }
  GET /dogs
    โœ• returns an HTTP 200 and a pagination result of docs (34 ms)

  โ— GET /dogs โ€บ returns an HTTP 200 and a pagination result of docs

    TypeError: Cannot read properties of undefined (reading '0')

      45 |
      46 |             // Assert: check the result
    > 47 |             expect(response.results[0]).toEqual(tradeLaneExample);
         |                                    ^
      48 |         });
      49 |     });
      50 | });

      at src/tradeLanes/tradelanes.test.pactv3.ts:47:36

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        2.436 s
Ran all test suites.
error Command failed with exit code 1.
info Visit <https://yarnpkg.com/en/docs/cli/run> for documentation about this command.
l
that makes so much more sense ๐Ÿ˜„ I should have spotted it! Thanks again ๐Ÿ™‚ This will build a new version of pact-js?
m
yep! Iโ€™ll get a release out tomorrow, if not earlier (there is a build running now, but we realised there is a flakey test that needs addressing)
l
check, thanks for the quick action!
๐Ÿ‘ 1
m
np - thanks for the repro, that always makes things a TONNE easier!
โœ… 1
1000000 1
P.S. I think you want the response to be:
Copy code
{
    results: [...]
}
right? Where each item in
results
is of the same shape?
If so,
Copy code
const EXPECTED_BODY = MatchersV3.eachLike({
    results: [tradeLaneExample]
});
should be
Copy code
const EXPECTED_BODY = {
    results: MatchersV3.eachLike(tradeLaneExample)
};
l
yeah, I had already found that one ๐Ÿ˜„ and that allowed me to generate the pact without issues
m
OK cool! Just checking ๐Ÿ™‚
๐Ÿค— 1
OK that flakey test (well, itโ€™s reliable in a way) broke the release - Iโ€™ll fix it tomorrow and get a release out when Iโ€™m more awake
โœ… 1
๐Ÿ‘‹
l
haha, sure sure ๐Ÿ˜„ thanks again!
y
Should be rocking now Lennart, PR here https://github.com/lennartquerter/pact-v3-test/pull/1
cc @Lennart Querter ๐Ÿ™Œ
m
Thanks for getting the release out Yousaf. Let us know how you go Lennart!
l
Thanks guys! I will test it right away ๐Ÿ˜„
๐Ÿ™Œ 2