no-global-tests

Description

Mocha gives you the possibility to structure your tests inside of suites using describe, suite or context. This rule aims to prevent writing tests outside test-suites. Example:

describe('something', function () {
    it('should work', function () {});
});

it('should not work', function () {});

Rule Details

This rule checks each mocha test function to not be located directly in the global scope.

Incorrect

it('foo');

test('bar');

it.only('foo');

test.skip('bar');

Correct

describe('foo', function () {
    it('bar');
});

suite('foo', function () {
    test('bar');
});

Caution

Caveats

It is not always possible to statically detect in which scope a mocha test function will be used at runtime. For example, let’s imagine a parameterized test which is generated inside a forEach callback:

function parameterizedTest(params) {
    params.forEach(function (i) {
        it('should work with ' + i, function () {});
    });
}

parameterizedTest([1,2,3]);

The rule won’t be applied in this case.