Skip to content

Latest commit

 

History

History
77 lines (62 loc) · 2.36 KB

no-setup-in-describe.md

File metadata and controls

77 lines (62 loc) · 2.36 KB

Disallow setup in describe blocks (mocha/no-setup-in-describe)

💼 This rule is enabled in the ✅ recommended config.

Setup for test cases in mocha should be done in before, beforeEach, or it blocks. Unfortunately there is nothing stopping you from doing setup directly inside a describe block.

describe('something', function () {
    const a = setup();
    const b = a.getter;
    it('should work', function () {
        assert(a === b);
    });
});

Any setup directly in a describe is run before all tests execute. This is undesirable primarily for two reasons:

  1. When doing TDD in a large codebase, all setup is run for tests that don't have only set. This can add a substantial amount of time per iteration.
  2. If global state is altered by the setup of another describe block, your test may be affected.

This rule reports all function calls and use of the dot operator (due to getters and setters) directly in describe blocks. An exception is made for Mocha's suite configuration methods, like this.timeout();, which do not represent setup logic.

If you're using dynamically generated tests, you should disable this rule.

Rule Details

This rule looks for all function calls and use of the dot operator which are nested directly in a describe block.

The following patterns are considered problems:

describe('something', function () {
    let a = b.c;
    it('should work', function () {});
});

describe('something', function () {
    const a = setup();
    it('should work', function () {});
});

These patterns would not be considered problems:

describe('something', function () {
    var a;
    beforeEach(function () {
        a = setup();
    });
    it('should work', function () {});
});
function getTest() {
    var a = setup(),
        b = a.someGetter;
    describe('something', function () {
        it('should work', function () {});
    });
}
describe('something', function () {
    function setup() {
        const a = setup(),
            b = a.someGetter;
        return { a, b };
    }
    it('should work', function () {
        const { a, b } = setup();
    });
});
describe('something', function () {
    this.timeout(5000);
    it('should take awhile', function () {});
});