Home > Software design >  Issue with a recursive function in Cypress. Tests slowing down over time
Issue with a recursive function in Cypress. Tests slowing down over time

Time:12-07

I am working on a diagnostic application which can ask the user quite a large number of questions (100 ). I had been using a recursive function to deal with the questions and their answering but i noticed over time the tests would slow down to a crawl and after a lot of research I've come to realize that its probably the recursion that's doing it. Any help would be much appreciated.

The current solution is this:

Cypress.Commands.add('intro_Questions', (data) => {
    const intro_Questions_Loop = () => {
        cy.get('@question_tree').then(question_tree => {//gets the current question tree
            cy.get('@link').then(link => { //get the question number
                if (action == 'LAST') {
                    return;
                }
                cy.Question({q: link, qt: question_tree, question_log: data.fixture}) //checks the question on screen vs question tree
                cy.Answer({q: link, qt: question_tree}) //picks a random answer and wraps action with the next question code
                cy.editAnswer({}) //edits questions at random dependant on set percentage
                intro_Questions_Loop();
            })
        })
    }
    intro_Questions_Loop();

})

Any help to improve, make it more efficient or just to write it in a different way would be much appreciated!

CodePudding user response:

Recursive functions can be memory-hogs, but 100 iterations isn't a large number.

Without seeing all the hidden details you haven't posted, I'd say the (main) problem is with the way you recurse into the synchronous function intro_Questions_Loop.

Try putting the recursive call on the Cypress command queue

...
cy.editAnswer({}) 
  .then(() => {
    intro_Questions_Loop()
  })

which should give the test runner time to process each iteration before getting the next.

Other than that, it's hard to say because too much of your actual code is hidden inside custom commands.

  • Related