pact-msw-adapter
pact-msw-adapter copied to clipboard
Allow Provider State to be set dynamically instead of hard code
Checklist
Before making a feature request, I have:
- [x] Searched the issues to check that this feature hasn't been requested before
- [x] Checked the documentation to see if it is possible to do what I want already
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.
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
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 ?
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();
});
See also https://github.com/pactflow/pact-cypress-adapter/pull/22.