Table of contents
1.
Introduction
2.
Retrying Tests
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Retrying Tests

Author Parth Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Mocha is a JavaScript framework that runs on Node.js and makes asynchronous testing significantly easy and fun. Mocha tests run serially, allowing for flexible and accurate reporting while mapping uncaught exceptions to the correct test cases.
This blog will help you understand what Retrying Tests are in MochaJS and its implementation.

Retrying Tests

When a test fails, it is still possible to attempt it a certain number of times. Mocha has a retry feature that enables it to handle end-to-end tests such as Selenium and functional tests.
It is important to note that the retry feature is not recommended for unit tests.
Additionally, while providing the ability to re-run a failed test, the retry feature does not run the before or after hooks. However, it will re-run the corresponding afterEach and beforeEach hooks.
To help understand the above statements, consider the code below.

describe('retries', function() {
  // this code will retry all the tests in this suite up to 2 times.
  this.retries(2);

  beforeEach(function() {
    browser.get('http://www.google.com');
  });

  it('has to succeed on the 2nd try', function() {
    // Specify that this test is to only retry up to 3 times
    this.retries(3);
    expect($('.foo').isDisplayed()).to.eventually.be.true;
  });
});

Let us consider another example:

describe('retries', function() {
  // this will retry all tests in this suite up to 4 times
  this.retries(4);

  beforeEach(function() {
    browser.get('http://www.codingninjas.com');
  });

  it('should succeed on the 2nd try', function() {
    // Specify this test to only retry up to 2 times
    this.retries(2);
    expect($('.foo').isDisplayed()).to.eventually.be.true;
  });
});

FAQs

  1. What is Mocha JS used for?
    Mocha is a feature-packed JavaScript testing framework that runs on Node.js, making asynchronous testing simple and efficient. 
  2. What is Mocha NodeJS?
    Mocha is a testing library for Node. Js, created to be simple, extensible, and rapid. It's used for unit and integration testing.
  3. What are Exclusive Tests?
    Exclusive Tests allow the developer to run only a specified test case or suite of tests with the help of the .only() function.
  4. What are Retrying Tests?
    When a test fails, it is still possible to attempt it a certain number of times. Mocha has a retry feature that enables it to handle end-to-end tests such as Selenium and functional tests.
  5. How to install Mocha JS?
    Mocha's installation process is straightforward. All we need to do is type 
    $ npm install --global Mocha to install Mocha globally inside the system.

Key Takeaways

In this article, we have extensively discussed Retrying Tests in Mocha JS and how they are used with practical examples. If you are Preparing for interview and don't know where to start, we have got you covered, check out our expert curated courses on our website, You can also check out Coding Ninjas Studio to practice frequently asked interview problems. We hope that this blog has helped you enhance your knowledge regarding Retrying Tests and if you would like to learn more, check out our articles. Do upvote our blog to help other ninjas grow. Happy Coding!”

Live masterclass