Skip to content

jest/require-top-level-describe Style

What it does

Requires test cases and hooks to be inside a top-level describe block.

Why is this bad?

Having tests and hooks organized within describe blocks provides better structure and grouping for test suites. It makes test output more readable and helps with test organization, especially in larger codebases.

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.

Examples

Examples of incorrect code for this rule:

javascript
// 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", () => {});

Examples of correct code for this rule:

javascript
// 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
    }
  ]
}

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny jest/require-top-level-describe --jest-plugin
json
{
  "plugins": ["jest"],
  "rules": {
    "jest/require-top-level-describe": "error"
  }
}

References

Released under the MIT License.