UI tests are flaky, especially when you run it on mobile devices. There are multiple factors that can lead to false-positive test results, if that happens you would want retry failed test before creating a bug.

In this blog post, we are going to add retry condition to Detox tests with Mocha, although this is a Mocha function and can be used with any other testing framework Appium, Selenium, etc.

Ok, here is our original test:

describe('My application spec', () => {

  this.retries(2);

  it('should have a login screen', async () => {
    await expect(element(by.id('LoginScreen'))).toBeVisible();
  });

});

So now we can add

this.retries(2);

and it should solve the problem. Well, not so fast, this doesn’t work in Mocha if you use arrow function (lambda), you would have to change it to a classic function before calling the retry. Here is how our final code will look like:

describe('My application spec', function() {

  this.retries(2);

  it('should have a login screen', async () => {
    await expect(element(by.id('LoginScreen'))).toBeVisible();
  });

});

Hope this post helps, feel free to post your questions, suggestions in the comments section below.