I'm using Cypress (https://www.cypress.io/) to test an app that keeps track of the amount of API calls are done within a user limit. The test checks the limit before the API call and again after a call. Purpose of the test is to see if the limits change after doing a call.
The limit is rendered on screen. And I try to store the value in a variable. After doing the API call I want to compare the value before and after.
Already tried to store it in a variable with const and let, but both don't work outside the 'it' statement.
it('should get the limit value before doing an api call', ()=> {
            cy.get('.bar__legend')
                .contains('used')
                .then(($usage) => {
                    let usageTxt = $usage.text()
                    let words = usageTxt.split(' ')
                    let usageBefore = words[0]
                })
        });
it('should do an API call twice', () => {
            // do a API call twice
}
it('should get the limit value after doing an api call', ()=> {
            cy.get('.bar__legend')
                .contains('used')
                .then(($usage) => {
                    let usageTxt = $usage.text()
                    let words = usageTxt.split(' ')
                    let usageAfter = words[0]
                    cy.log(usageBefore)
                    cy.log(usageAfter)
                })
        });
Another approach I tried
it('should increase the limit after an api call', ()=> {
            cy.get('.bar__legend')
                .contains('used')
                .then(($usage) => {
                    let usageTxt = $usage.text()
                    let words = usageTxt.split(' ')
                    let usageBefore = words[0]
                })
            cy.visit('apilink')
            cy.wait(2000)
            cy.visit('apilink')
            cy.wait(2000)
            cy.get('.bar__legend')
                .contains('used')
                .then(($usage) => {
                    let usageTxt = $usage.text()
                    let words = usageTxt.split(' ')
                    let usageAfter = words[0]
                    cy.log(usageBefore)
                    cy.log(usageAfter)
                })
        })
I expected a value for both variables, but the test fails, because the 'usageBefore' variable does not exist.
 
     
    