Check if feature flag is on or off before before Cypress test runs

1.6k Views Asked by At

I am trying to write tests for features that aren't enabled yet (hidden behind feature flag); There's an endpoint I can call to check if the application feature flag is turned on or off and I wrote a function to do so.

My next task is to implement a way for Cypress to only run specific tests if the feature flag is ON. How do I implement that leveraging the method I wrote? Is there a check I have to implement somewhere in the Cypress.config?

Thanks!

code for the method

checkTemplateFeatureFlag(): Chainable<Temp> {
  return (new Cypress.Promise(resolve => {
    cy.request({
      method: 'GET',
      url: `ENDPOINT_HERE`
    }).then(resp => {
      const array = JSON.parse(resp.body);
      const foundFlag = !!array.find(
        (obj: { flag_name: string; active: boolean }) =>
          obj.flag_name === 'FLAG_NAME' && obj.active
      );
      resolve(foundFlag);
    });
  }) as unknown) as Chainable<Temp>;
}
1

There are 1 best solutions below

0
On

A simple way to skip tests (or describe blocks) dynamically is to use function() form callbacks to access this, then conditionally call this.skip().

You will also need to call checkTemplateFeatureFlag() in a separate block, say a beforeEach(), since it has async internals.


let skipFeature;
beforeEach(() => {
  checkTemplateFeatureFlag().then(foundFlag => skipFeature = !foundFlag)
})

it('tests the feature', function() {  // function here for "this" access
  if (skipFeature) this.skip()
  ...
})