Skip to content

jest/require-top-level-describe Style

What it does

This rule triggers a warning if a test case (test and it) or a hook (beforeAll, beforeEach, afterEach, afterAll) is not located in a top-level describe block.

Example

javascript
// invalid

// Above a describe block
test("my test", () => {});
describe("test suite", () => {
  it("test", () => {});
});

// Below a describe block
describe("test suite", () => {});
test("my test", () => {});

// Same for hooks
beforeAll("my beforeAll", () => {});
describe("test suite", () => {});
afterEach("my afterEach", () => {});

//valid

// Above a describe block
// In a describe block
describe("test suite", () => {
  test("my test", () => {});
});

// In a nested describe block
describe("test suite", () => {
  test("my test", () => {});
  describe("another test suite", () => {
    test("my other test", () => {});
  });
});

Options

You can also enforce a limit on the number of describes allowed at the top-level using the maxNumberOfTopLevelDescribes option:

json
{
  "jest/require-top-level-describe": [
    "error",
    {
      "maxNumberOfTopLevelDescribes": 2
    }
  ]
}

References

Released under the MIT License.