Mocha: this.retries setting is not working on beforeEach.

Created on 27 Apr 2018  路  8Comments  路  Source: mochajs/mocha

Description
I'm setting retry time in function beforeEach with method this.retries(1), but it seems it's not working.
describe('Retry', () => {

beforeEach(function() {
    this.retries(1);
    console.log('before each, set retry to 1');
});

it('should retry 1 time', function() {
    console.log('tests');
    expect(false).to.be.true;
})

})

Expected behavior: The test should be retry 1 time because it's setting in beforeEach.

Actual behavior: The test is failed without any retry.

Reproduces how often: always

Versions
mocha: 4.1.0
node: 8.9.4
MacOS 10.13.3

question

All 8 comments

See documentation on retry-tests and arrow-functions. Believe your problem came from use of arrow function in your describe().

Example:

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

    beforeEach(function () {
        browser.url('http://www.yahoo.com');
    });

    it('should succeed on the 3rd try', function () {
        // Specify this test to only retry up to 2 times
        this.retries(2);
        console.log('run');
        expect(browser.isVisible('.foo')).to.eventually.be.true;
    });
});

@plroebuck Thank you for you quick help, but it still doesn't help me.

describe('Retry',function() {

beforeEach(function() {
this.retries(1);
console.log('before each, set retry to 1');
});

it('should retry 1 time', function() {
console.log('tests');
expect(false).to.be.true;
})
})

I suppose that we do not support set retries in beforeEach?
Why I want to do that? For my testing app, it have an issue that the first screen is white screen. So I wanna assert in beforeEach, if the first screen is white, I want to retry this case.

If you consider the effect, there _is_ no point to setting retries over and over to the same value, when it could be done once. This achieves the same thing:

describe('Retry', function () {

  this.retries(1);
  console.log('describe, set retry to 1');

  beforeEach(function () {
    console.log('before each');
  });

  it('should retry 1 time', function () {
    console.log('test');
    expect(false).to.be.true;
  });
});

Don't assert in beforeEach() -- use that as you would JUnit.setup().
Do assertion testing in your test (a.k.a. it()).

Assuming this addresses your problem, can we close the issue?

this.retries() in a beforeEach() will retry the hook if it fails.

...in theory. IMO, that doesn't make much sense. @plroebuck's solution should work.

Please reopen if it doesn't

Thanks guys.

I'm clear now.
If this.retries() in beforeEach(), it will retry the hook not test.

I'll find another way to work it out.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

helloravi picture helloravi  路  3Comments

CADBOT picture CADBOT  路  3Comments

adamhooper picture adamhooper  路  3Comments

Swivelgames picture Swivelgames  路  3Comments

danielserrao picture danielserrao  路  3Comments