ng-apimock / cypress-plugin

The cypress plugin for ng-apimock.
MIT License
6 stars 3 forks source link

WebApi calls of API aren't scoped to session and thus parallel tests are flaky #353

Closed GrumpyMeow closed 3 years ago

GrumpyMeow commented 3 years ago

HI,

We're using ng-apimock for mocking for our Angular apps. In the CICD-pipeline we're doing a parallel Cypress E2E tests of our apps. We're experiencing flaky test-runs in this situation.

I think i've tracked it down to the fact that the webapi-calls that our app is making don't have the apimockid-cookie, while the apimockid-cookie is present on the ng-apimock-api-calls.

Can you maybe explain to me how the apimockid-cookie is expected to be added to the webapi-calls of our apps? I did some experimenting with adding a new method to a fork of your code to set the ngapimock-cookie in the context of the Cypress-test:

    cy.setSessionCookie();    <--- Call of my new method

    cy.selectPreset('happy-flow');
    cy.selectScenario('getData', 'onboarded');
    cy.visit('/', { log: false });
     setSessionCookie(): Promise<any> {
        return new Cypress.Promise((resolve, reject) => {
            cy.setCookie(this.configuration.identifier, this.ngApimockId);
            resolve();
        });
    }

thanks in advance! groetjes, Sander

GrumpyMeow commented 3 years ago

I've found a good workaround. The selectPreset/selectScenario commands return information which exposes the apimockid cookie value. By setting the apimockid-cookie also on the domain you're making api-calls to, these api-calls will be in the same ngapimock-scope. This will make sure that the selected preset/scenario is actually used for returning the mocks. This will also allow for running multiple Cypress E2E tests in parallel.

cy.selectPreset('happy-flow').then((apiresult)=>{
   const cookieheader = apiresult.requestHeaders.Cookie;
   const cookies = cookieheader.split(';');
   cookies.forEach((cookiestr: string) => {
      const cookieparts = cookiestr.split('=');
      if (cookieparts[0] == 'apimockid') {
         cy.clearCookie('apimockid', { log: false });
         cy.setCookie(cookieparts[0], cookieparts[1], { httpOnly: true, log: false });
         Cypress.Cookies.preserveOnce(cookieparts[0]);
      }
   }
});