Hello everyone, I'm trying to setup a small Types...
# pact-js
a
Hello everyone, I'm trying to setup a small Typescript project to use Pact, The test is setup as follows: • a Docker container with the app and the test • a separate Docker container where the mock server is running It seems that PactJS assumes I want to setup the mock server in the same place as the app, which is not possible since Ruby is not installed. Is there a way around it? If I do cURL from the app container, I get this:
Copy code
docker-compose run --rm node-cli curl -X GET pact-mock-provider:1234
Creating rover_node-cli_run ... done
{"message":"No interaction found for GET /","interaction_diffs":[]}
But if I run the tests, I get the following:
Copy code
docker-compose run --rm node-cli npm run test:mocha:consumer
Creating rover_node-cli_run ... done

> test:mocha:consumer
> mocha src/consumer.spec.ts

[2022-03-22 17:21:28.234 +0000] INFO (18 on node-cli): pact-node@10.17.2: Creating Pact Server with options: 
{"timeout":30000,"consumer":"testingConsumer","cors":false,"dir":"/opt/project/roverctl/pacts","host":"pact-mock-provider","log":"/opt/project/roverctl/stderr","logLevel":"DEBUG","pactfileWriteMode":"overwrite","provider":"testingProvider","spec":2,"ssl":false,"port":1234,"pactFileWriteMode":"overwrite"}
(node:18) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)


  GraphQL example
    1) "before all" hook in "GraphQL example"
[2022-03-22 17:21:28.248 +0000] ERROR (18 on node-cli): pact@9.17.3: The pact mock service doesn't appear to be running
  - Please check the logs above to ensure that there are no pact service startup failures
  - Please check that pact lifecycle methods are called in the correct order (setup() needs to be called before this method)
  - Please check that your test code waits for the promises returned from lifecycle methods to complete before calling the next one
  - To learn more about what is happening during your pact run, try setting logLevel: 'DEBUG'
    2) "after all" hook in "GraphQL example"


  0 passing (11ms)
  2 failing

  1) GraphQL example
       "before all" hook in "GraphQL example":
     Error: listen EADDRNOTAVAIL: address not available 172.28.0.4:1234
      at Server.setupListenHandle [as _listen2] (node:net:1355:21)
      at listenInCluster (node:net:1420:12)
      at GetAddrInfoReqWrap.doListen (node:net:1559:7)
      at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:74:8)

  2) GraphQL example
       "after all" hook in "GraphQL example":
     Error: The pact mock service wasn't running when finalize was called
      at Pact.finalize (node_modules/@pact-foundation/src/httpPact.ts:183:9)
      at Context.<anonymous> (src/consumer.spec.ts:33:26)
      at processImmediate (node:internal/timers:466:21)
m
You don’t need Ruby to run Pact. It’s bundled in the npm package and should be invisible to you
I wouldn’t recommend this setup
Why did you think you needed ruby?
a
The errors I was getting hinted me in that direction. In any case, I'm happy to report I managed to make it work, but instead the
new Pact
used in the graphQL example I linked, I used
new PactWeb
and it worked nicely with the rest of the example. Thank you for your feedback so far @Matt (pactflow.io / pact-js / pact-go)
m
hmm that’s strange
you shouldn’t need to use PactWeb, and we’re currently planning on decommissioning/stop support for it going forward
That would indicate there are issues starting the mock server - it could even be a port conflict
setting log level to debug and sharinsg the output might be helpful
a
it could even be a port conflict
I think it's unlikely. The mock server is brought up outside the node ecosystem. From this part of the message I posted above:
Copy code
Error: listen EADDRNOTAVAIL: address not available 172.28.0.4:1234
I get the feeling Node is trying to start the server without checking if the server is already reachable.
setting log level to debug and sharinsg the output might be helpful
I'll try to recreate the situation further.
you shouldn’t need to use PactWeb, and we’re currently planning on decommissioning/stop support for it going forward
The
PactWeb
I'm refering still part of the
@pact-foundation/pact
package, not the
@pact-foundation/pact-web
m
The mock server is brought up outside the node ecosystem.
the port conflict has nothing to do with where the conflict happens - in or out or node
I’d like to see your configuration for Pact vs how you are starting the mock server yourself
This looks like a different internal network. Are you hard coding the port to
1234
? If so, you need to make sure you can a) bind to that port b) it’s free and c) the address you want to bind is corrrect.
172.28.0.4:1234
Looks like you’re not using dynamic port allocation (i.e. you’re hard coding
1234
as the port). Drop the hard coding and it should pick a random free one for you
a
Hi Matt, here is the docker-compose I'm using:
Copy code
services:
  app:
    build:
      context: /home/agustingomes/pact-project-poc
      dockerfile: ./.docker/Dockerfile.node.alpine
    container_name: contract-test
    environment:
      PACT_CONSUMER_NAME: instapro-client
      PACT_MOCK_SERVER_HOST: 127.0.0.1
      PACT_MOCK_SERVER_PORT: '1234'
      PACT_PROVIDER_NAME: pocProvider
    hostname: contract-test
    network_mode: host
    volumes:
      - /home/agustingomes/pact-project-poc/contract-tests-poc:/var/www/html:rw
    working_dir: /var/www/html
  pact-mock-provider:
    command: >
      mock-service
      --port 1234
      --host 0.0.0.0
      --pact-dir=/tmp/pacts
      --consumer=pocConsumer
      --provider=pocProvider
      --log=/tmp/pacts/mock-provider.log
      --log-level=debug
      --pact-specification-version=2.0.0
      --pact-file-write-mode=overwrite
    container_name: pact-mock-provider
    expose:
      - '1234'
    hostname: pact-mock-provider
    image: pactfoundation/pact-cli:0.50.0.19
    ports:
      - published: 1234
        target: 1234
    volumes:
      - /home/agustingomes/pact-project-poc/pacts:/tmp/pacts:rw
    working_dir: /pact
version: '3.8'
I start the Mock provider with:
Copy code
docker-compose up -d pact-mock-provider
And then, I run the test via the
app
service I defined in my docker-compose:
Copy code
docker-compose run --rm app npm run test:contract
And finally, this is how the provider in the test is set up:
Copy code
import { PactWeb } from "@pact-foundation/pact/src/pact-web"

.
.
.

const provider = new PactWeb({
        host: process.env.PACT_MOCK_SERVER_HOST || "",
        port: parseInt(process.env.PACT_MOCK_SERVER_PORT || ""),
        log: "stderr",
        logLevel: "debug",
    })

.
.
.
With this setup, I am able to generate the contract:
Copy code
> mocha src/consumer.spec.ts
  
  Setting up Pact using mock service on port: "1234"
  (node:18) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
    
    
    GraphQL login
    login via /graphql
      ✔ returns a valid token
    
    1 passing (27ms)
Let me know if I can provide you more details
m
Look, you can do it that way, but as I said, we’re going to be removing the
PactWeb
interface going forward, and it’s a clumsy setup. I’d like to know why you need to manually run the mock service and you can’t just run the tests
if you could share a reproducible code base, I could run it to see why it doesn’t work
You can do what you’re doing above, I’m just giving you the forewarning that it means you will unlikely be able to upgrade to the next major release without rework
a
if you could share a reproducible code base, I could run it to see why it doesn’t work
I will put together the example I have. Will do it in 2 commits. the first one with
PactWeb
, and the second with
Pact
.
🙌 1
m
Thanks!
note I’m off for a few days, but if you raise an issue at the pact-js repo I can take a look when back (or another maintainer might be able to check also)
a
no rush. Enjoy your days off @Matt (pactflow.io / pact-js / pact-go) and thanks for bearing with me 🙌
👍 1