Hi folks, I'm trying to get provider tests to work...
# pact-js
m
Hi folks, I'm trying to get provider tests to work on my backend and I'm having trouble understanding how to get Pact to send requests on the correct port that I'm running my Express test server on. Basically I'm doing this: • Start my server using
app = await setupApp()
which brings up an instance of the Express server on localhost using port
3030
• And in the provider opts I'm passing port
3030
through the
providerBaseUrl
But when I run the tests, I get a connection error on port
3030
and I see that Pact has actually sent a request to a random port (e.g.
54127
). The most curious thing is that we already do this in our unit tests to run instances of Express, Mongo and so on using Jest, and it works perfectly. I'm leaving here the error logs and also the test code.
Copy code
let app: Express

let locationCollection: CollectionDecorator

const fakeLocation = {
  name: 'Fake Location',
  description: 'Fake Location Description',
  address: 'Fake address',
  createdAt: new Date(),
  updatedAt: new Date(),
  createdByUserId: new ObjectID().toHexString(),
  image: 'fake_image',
}

describe('Location Provider Contract Test', () => {
  beforeAll(async () => {
    app = await setupApp()
    await MongoHelper.connect(process.env.MONGO_URL)
  })

  afterAll(async () => {
    await shutdownDependencies()
  })

  it('should return 200 when getting a location by id', async () => {
    const opts = {
      provider: 'motor',
      providerBaseUrl: '<http://localhost:3030>',
      pactBrokerUrl: process.env.PACT_BROKER_URL,
      pactBrokerToken: process.env.PACT_BROKER_TOKEN,
      publishVerificationResult: true,
      providerVersion: '0.0.1',
      consumerVersionSelectors: [
        {
          consumer: 'webapp',
          tags: ['0.0.2'],
          latest: true,
        },
      ],
      logLevel: 'debug' as const,
      stateHandlers: {
        hasLocationWithSomeId: async () => {
          const location = (
            await locationCollection.insertOne(fakeLocation)
          ).ops[0]
          await request(app).get(`/locations/${location._id}`)
        },
      },
    }

    return await new Verifier(opts).verifyProvider()
  })
})
Error screenshots
m
Pact spins up an internal proxy in order to support a number of capabilities, hence why you see requests to a random port
The connection refused on
::1
seems to indicate that your API is not bound to an ipv6 address, but the OS thinks
localhost
is an ipv6 address. You could try using
127.0.0.1