Hello, I am seeing some weird behaviour but I am n...
# pact-js
j
Hello, I am seeing some weird behaviour but I am not able to find what I am doing wrong. Basically I have a contract already created and I am trying to test the provider side using
"@pact-foundation/pact": "^9.17.3"
This is my contract. It is already in the pact broker.
Copy code
{
   "method": "POST",
   "path": "/files",
   "headers": {
     "Content-Type": "application/json"
   },
   "body": {
     "clientId": "2454ad95-0352-4494-9a40-6afb333959e4",
     "fileContent": "xxxxofAAABAE=",
   }
}
In the provider side, I just created a test and added routes. Until here, everything is ok. The weird part is that when the verification happens with
new Verifier(options).verifyProvider()
, it calls the business logic code without the body. The other properties (baseUrl, method), they are passed to the the code, but not the body.
Copy code
<http://router.post|router.post>('/', (req, res, next) => {

        console.log("req.baseUrl", req.baseUrl)
        console.log("req.path", req.path)
        console.log("req.method", req.method)
        console.log("req.body", req.body)

})
The code above, generate this logs:
Copy code
req.baseUrl /files
req.path /
req.method POST
req.body undefined
Not sure why the body is now being sent to the endpoint being tested. I have done other contract tests, but all without request body and I never notice this before. Anyone has any ideas of what I could be doing wrong?
PS: I am running it using
"jest-pact": "^0.9.4"
This is the testing class, removed everything and left the simplest
Copy code
const { Verifier } = require('@pact-foundation/pact')
const express = require('express')
const pactBrokerUrl = "<https://pact-brokers.tools.XX.XX>"
const providerBaseUrl = '<http://localhost:3000/>'

const app = express()
const server = app.listen(3000, () => {
    console.log("Service Listening")
})


<http://app.post|app.post>('/files', (req, res, next) => {

    console.log("req.baseUrl", req.baseUrl)
    console.log("req.path", req.path)
    console.log("req.method", req.method)
    console.log("req.body", req.body)

    throw Error("only a test")

})

const options = {
    provider: "file-service",
    providerBaseUrl,
    pactBrokerUrl,
    logLevel: "debug",
    consumerVersionTags: ["merge-requests/5984"],
};

describe("Pact Verification", () => {
    test("validates the expectations", () => {
        return new Verifier(options).verifyProvider()
            .finally(() => { server.close() })
    })
})
and this is the script that I run
"test:pactProvider": "jest -c jest.contracts.provider.config.js --runInBand"
t
I would guess the problem is in the consumer test, not the verification
can you share your consumer test code?
Are you deleting your pacts in between test runs?
It might be that you had an old interaction without the body that is still in the file
m
My guess is that you haven’t enabled the JSON body parser on your express app, and it’s not automatically parsing the body into JSON for you
💯 2
❤️ 1
Note the use of the
express.json()
middleware to automatically parse the body and make it available on the
req
object:
Copy code
var express = require('express')

var app = express()

app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

<http://app.post|app.post>('/profile', function (req, res, next) {
  console.log(req.body)
  res.json(req.body)
})
❤️ 1
t
oh man, I thought they fixed that 😞
good pickup
m
This is the kind of thing you learn from experience!
Given it’s just built on the underlying Node http APIs, the issue is that express would need to guess in advance you want to consume the incoming byte stream into a particular format (e.g. JSON). This can be expensive, so is probably why they chose not to by default. But I imagine the 99/100 scenario is that people want to be able to parse JSON
t
For me, the problem is that it's set to undefined and not some error type
m
yes, good point
t
or, better - a raw buffer
(sorry for cluttering the thread with my complaints about express)
😆 1
j
I LOVE YOU GUYS! you made my day! I spent the whole day today trying to figure this out. It was what @Matt (pactflow.io / pact-js / pact-go) said, I did not had
app.use(express.json())
middleware. Thank you so much @Matt (pactflow.io / pact-js / pact-go) and @Timothy Jones for the help!
🙌 1
t
Haha! You're very welcome! Glad you got it working
m
You’re welcome - it’s awesome helping somebody yesterday, today 😆
🥰 1
Imagine how productive I could be, if I could fix things yesterday?!
❤️ 1
j
Only someone from the future could have an insight like that lol2
😆 2