Hi, I have the following code to generate a messag...
# pact-js
s
Hi, I have the following code to generate a message consumer pact:
Copy code
const path = require('path');
const {
    MatchersV3: {like, regex},
    MessageConsumerPact,
    SpecificationVersion,
    asynchronousBodyHandler,
} = require('@pact-foundation/pact');

const {KAFKA_TOPIC, streamHandler} = require('../kafka/consumer');


const messagePact = new MessageConsumerPact({
    consumer: process.env.CONSUMER || 'AuditService',
    provider: 'CardService_Async',
    logLevel: 'warn',
    dir: path.resolve(process.cwd(), 'pacts'),
    spec: SpecificationVersion.SPECIFICATION_VERSION_V3,
    pactfileWriteMode: 'update',
});


describe('Kafka Pact test', () => {
    describe('receive an audit log message', () => {
        test('accepts a message', () => {
            messagePact
                .expectsToReceive('a message event update')
                .withMetadata({
                    'content-type': 'application/json',
                    'x-kafka-topic': KAFKA_TOPIC,
                })
                .withContent({
                    viewer: like('John Doe'),
                    card: {
                        id: like('08'),
                        type: regex(/^(CREDIT_CARD|PERSONAL_LOAN)$/, 'CREDIT_CARD'),
                        name: like('MyFlexiPay'),
                        version: like('v1'),
                    },
                })
                .verify(asynchronousBodyHandler(({content}) => streamHandler(content)));
        });
    });
});
And the following verification code:
Copy code
const commonVerifierConfig = (commonEnv) => {
    return {
        logLevel: 'warn',
        providerVersion: commonEnv.GIT_COMMIT_SHA,
        providerVersionBranch: commonEnv.GIT_BRANCH,
        publishVerificationResult: commonEnv.PUBLISH_VERIFICATION_RESULTS,

        // Similar to state handlers?
        messageProviders: {
            'a message event update': () => JSON.parse(buildMessage(
                'John Doe',
                {id: '08', type: 'CREDIT_CARD', name: 'MyFlexiPay', version: 'v1'},
            ).value),
        },
    };
};

const verifySinglePact = (commonEnv) => () => {
    it('validates the expectations of CardService', async () => {
        // Initialize the Pact verifier
        const verifier = new MessageProviderPact({
            ...commonVerifierConfig(commonEnv),
            pactUrls: [process.env.PACT_URL],
        });

        // Verify pacts
        const output = await verifier.verify();
        console.log(output);
    });
};
with
buildMessage
being:
Copy code
const buildMessage = (viewer, card) => ({
    value: JSON.stringify({viewer, card}),
    headers: {
        'content-type': 'application/json',
        'x-kafka-topic': KAFKA_TOPIC,
    },
});
The validation for the message body passes, but the one for the message metadata fails. I have attached the generated contract.
even though
metadata
is present in the contract, it shows
undefined
in Pactflow
In my pact (json) file, it says
4.0.0
for
pactSpecification
, even though I set
spec: SpecificationVersion.SPECIFICATION_VERSION_V3,
when generating it. In my pact (v4),
metadata
is spelt with lowercase
d
, whereas in Pactflow and v3 example (see screenshot) it is spelt with capital
D
->
metaData
@Matt (pactflow.io / pact-js / pact-go)
I think the example in the doc with
metaData
is old .... when using
spec: specificationVersion.SPECIFICATION_VERSION_V2
I get this in the pact:
Copy code
"metadata": {
        "content-type": "application/json",
        "contentType": "application/json",
        "x-kafka-topic": "audit-log"
      }
m
He Stefan, thanks for the digging. I’m pretty sure
metadata
is the correct key, not
metaData
It’s possible Pactflow is not properly handling/parsing the V4 contract. See also https://github.com/pact-foundation/pact-specification/tree/version-4#asynchronousmessages In any case, would you mind please raising a bug so we can investigate? There are a few things going on here that will need investigating
m
Thank you!