0

How can a data value be updated after the new value is fetched using cy.intercept()?

let data = [
  {
    track: 'Track 1',
    case: [{name: 'case 1.1'}, {name: 'case 1.2'}],
  },
];

describe(`All test`, {tags: '@testConcept'}, () => {
  beforeEach(() => {
    // LOGIN and fetching new data and updating 
    data = [
      {
        track: 'Track 3',
        case: [{name: 'case 3.1'}, {name: 'case 3.2'}],
      },
    ];
  });

  data.forEach((el) => {
    describe(`Test :`, () => {
      it(`Test Track :  ${JSON.stringify(el.track)}`, () => {
        cy.log(el.track); // expected Track 3
      });

      el.case.forEach((el2) => {
        it(`Test case :  ${JSON.stringify(el2.name)}`, () => {
          cy.log(el2.name); // expected Track 3.1
        });
      });
    });
  });
});

The data variable is getting hosted and the value is not getting changed

Attaching image on how the test look like enter image description here

0

1 Answer 1

0

From this answer Pre-scan web page for dynamic tests, it is clear you would need to do the fetch data in the before:run hook.

There is no possibility to change the number of it() calls in the test while the test is running, even in a beforeEach() hook.

cypress.config.js

const { defineConfig } = require("cypress");
const fetch = require('node-fetch');

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('before:run', async (details) => {
        try {
          const response = await fetch(data-url-here);  
          const data = await response.json();   
          config.env.data = data;           

        } catch (error) {
          console.log('error:', error)
        }
      })
      return config;
    },
  },
})

generalized pattern for data-driven tests

let data = [
  {
    track: 'Track 1',
    case: [{name: 'case 1.1'}, {name: 'case 1.2'}],
  },
];
const newData = Cypress.env('data')
data = [...data, ...newData]

describe(`All test`, {tags: '@testConcept'}, () => {

  data.forEach((el) => {
    describe(`Test :`, () => {
      it(`Test Track :  ${JSON.stringify(el.track)}`, () => {
        cy.log(el.track); // expected Track 3
      });

      el.case.forEach((el2) => {
        it(`Test case :  ${JSON.stringify(el2.name)}`, () => {
          cy.log(el2.name); // expected Track 3.1
        });
      });
    });
  });
})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.