pact-msw-adapter icon indicating copy to clipboard operation
pact-msw-adapter copied to clipboard

Allow Provider State to be set dynamically instead of hard code

Open TKDMaster12 opened this issue 3 years ago • 4 comments

Checklist

Before making a feature request, I have:

Feature description

I have a bunch of pacts for one project. I would like to add states to make the provider side of the pact work better.

TKDMaster12 avatar Oct 25 '22 20:10 TKDMaster12

The relevant line of code is here, providerState isn't set

https://github.com/pactflow/pact-msw-adapter/blob/bdae1c64fb996d1820e8223dbf6e258e77a21991/src/convertMswMatchToPact.ts#L20

Feel free to drop a PR or suggest how you wish the provider state to be set, as in what information you actually want it it.

I hadn't built the tool with the idea of working with providerStates/matchers to work with traditional CDCT testing, but would be happy to see someone extend the tool to do so

YOU54F avatar Oct 26 '22 12:10 YOU54F

We are on the same boat as @TKDMaster12 😄 We would like to have provider states passed in. Happy to push a PR to get this feature in.

But was wondering if you have any thoughts on how we might pass the states in. Given this gets initialised early on, I believe we lose context from an individual test point of view.

I was wondering if we can pass a generic provider state PER pactMswAdapter for a given provider. Hope that makes sense. Would love some brainstorming on how we can do it to see if I'm missing anything. Thoughts ?

jaswanthm avatar Dec 19 '22 06:12 jaswanthm

I figured out a work a round. I added an extra header to the calls. Then in the afterAll method pull that header and use that as the state. Hope this helps.

afterAll(async () => {
    if (pactMswAdapter) {
        await pactMswAdapter.writeToFile((path, data) => {

            let name = data.provider.name.split("-");

            data.interactions.forEach((interaction, index) => {
                if (name[name.length - 1] === "questions") {
                    let state = interaction.request.headers['extra-header-state'];
                    interaction.providerState = `${name[name.length - 1]}_${state}_${index}`;
                } else {
                    interaction.providerState = `${name[name.length - 1]}_${index}`;
                }
            });
            fs.writeFileSync(path, JSON.stringify(data));
        }); // writes the pacts to a file
        pactMswAdapter.clear();
    }
    server.close();
});

TKDMaster12 avatar Dec 19 '22 14:12 TKDMaster12

See also https://github.com/pactflow/pact-cypress-adapter/pull/22.

mefellows avatar Mar 06 '23 06:03 mefellows