pactflow / pact-cypress-adapter

Cypress Pact Plugin
MIT License
25 stars 13 forks source link

Unable to write multiple request intercepts to pact file #44

Open kand opened 2 months ago

kand commented 2 months ago

Hello! I have a test that's something like this:

describe('test some UI', () => {
  before(() => cy.setupPact('consumer-ui', 'provider-api');

  it('User is able to do a process that causes two requests I want to record', () => {
    cy.intercept('POST', '/api/thing-one').as('thing-one');
    cy.intercept('POST', '/api/thing-two').as('thing-two');

    // ... interact with UI

    cy.usePactWait('thing-one').then((xhr) => {
      expect(xhr.response.statusCode).to.eq(200);

      // ... UI is in a new state, interact with it again to get the second request
      cy.usePactWait('thing-two').then((xhr) => {
        expect(xhr.response.statusCode).to.eq(200);
      });
    });
  });
});

At this point, I have a pact file that only includes the last interaction for thing-two and not the interaction for thing-one. I believe this is because constructPactFile is excluding "duplicate" interactions based on the test title, which in this case, the test has two interactions under the same test title, so the first one gets excluded/overwritten.

Am I using the Cypress adapter correctly? If so, is there a reason I can only write one interaction per test? Or is this a bug in the adapter?

Thanks!

kand commented 2 months ago

I think the interface would make intuitive sense if the formattedAlias was included in testCaseTitle here:

https://github.com/pactflow/pact-cypress-adapter/blob/ac177d75dd75e16bef8c55986125780650357ee7/src/index.ts#L60

Which would match the behavior of using cy.usePactWait with an array of aliases.

nivedhasamy commented 2 months ago

I am facing the same issue. Did you manage to resolve this?

kand commented 2 months ago

Because of the issue in the adapter code I referenced in my last comment, I think the only way to resolve this is to separate the tests. So instead of

Test
- setup scenario A
- test result A
- setup scenario B now that we're in A
- test result B

I split it into two tests:

Test A
- setup scenario A
- test result A

Test B
- setup scenario A
- setup scenario B now that we're in A
- test result B

Kind of a bummer since this is not how Cypress works. Probably better test design though.