I am trying to manage the state of provider servic...
# pact-js
s
I am trying to manage the state of provider service before running provider verification and my state handlers seems like not invoked which fails my test. I am not sure what wrong I am doing. Can somebody help me here?
Copy code
const { Verifier } = require('@pact-foundation/pact')
const path = require('path')
const { beforeAll } = require('jest-circus')
const PORT = 8090
async function createProfile(from) {
    return await axios.post(`<http://localhost>:${PORT}/v2/profile/person/`, create_person_payload(), {
        headers: { 'Content-Type': 'application/json' },
        httpAgent: http.Agent({ keepAlive: false }),
    })
}
describe('Pact provider Verification', () => {
    const opts = {
        provider: 'ProfileService',
        logLevel: 'info',
        providerBaseUrl: '<http://localhost:8090>',
        pactUrls: [path.resolve(__dirname, '../pacts/MainApp-PersonProfile.json')],
        stateHandlers: {
            'provider is ready to create a person profile': async () => {
                // Set up necessary state for creating a person profile
                return Promise.resolve('Provider state setup successful')
            },
            'provider has a person profile': async () => {
                //for an update to happen one profile need to be exists. Creat a profile first
                await createProfile('PROVIDER')
                return Promise.resolve('Provider state setup successful')
            },
            'provider wanst to update the profile': async () => {
                //for an update to happen one profile need to be exists. Creat a profile first
                await createProfile('PROVIDER')
                return Promise.resolve('Provider state setup successful')
            },
        },
    }

    it('should validate the expectations of ProfileService', () => {
        return new Verifier(opts).verifyProvider().then((output) => {
            console.log(output)
        })
    })
})
My provider service is running locally at port 8090. My get and Patch Call fails as no data is available in DB. I want to call the state handler before verification which will call
createProfile
function which will call an api to create resources.
m
What makes you think they aren't invoked? What do the logs say?
s
I am checking in my local DB and there is no entry. Also logs say empty state invoked
Copy code
2024-07-22T11:03:11.213325Z  INFO ThreadId(13) pact_verifier: Running setup provider state change handler with empty state for 'A GET Request'
[16:33:11.495] WARN (73205): pact@13.1.0: no state handler found for state: ""
2024-07-22T11:03:11.505750Z  INFO ThreadId(13) pact_verifier: Running provider verification for 'A GET Request'
2024-07-22T11:03:11.505900Z  INFO ThreadId(13) pact_verifier::provider_client: Sending request to provider at <http://127.0.0.1:59665/>
m
2024-07-22T110311.213325Z INFO ThreadId(13) pact_verifier: Running setup provider state change handler with empty state for ‘A GET Request’
there is a state called
A GET Request
but your statehandlers don’t match. Whatever is in the consumer pact file must match your setup. The empty
""
state probably isn’t a problem, I’d like to see more evidence of other state handlers not firing that match your description
s
@Matt (pactflow.io / pact-js / pact-go) Can you guide me to some tutorials where I can get a clear understanding of how to manage state?