Skip to content
← Back to rules

jest/max-nested-describe Style

它的作用

此规则强制限制嵌套 describe() 调用的最大深度。

为什么这不好?

describe() 块嵌套得太深会让测试套件难以阅读和理解。

示例

以下模式被视为警告(默认选项为 { "max": 5 } ):

此规则的错误代码示例:

javascript
describe("foo", () => {
  describe("bar", () => {
    describe("baz", () => {
      describe("qux", () => {
        describe("quxx", () => {
          describe("too many", () => {
            it("should get something", () => {
              expect(getSomething()).toBe("Something");
            });
          });
        });
      });
    });
  });
});

describe("foo", function () {
  describe("bar", function () {
    describe("baz", function () {
      describe("qux", function () {
        describe("quxx", function () {
          describe("too many", function () {
            it("should get something", () => {
              expect(getSomething()).toBe("Something");
            });
          });
        });
      });
    });
  });
});

此规则的正确代码示例:

ts
describe("foo", () => {
  describe("bar", () => {
    it("should get something", () => {
      expect(getSomething()).toBe("Something");
    });
  });
  describe("qux", () => {
    it("should get something", () => {
      expect(getSomething()).toBe("Something");
    });
  });
});

describe("foo2", function () {
  it("should get something", () => {
    expect(getSomething()).toBe("Something");
  });
});

describe("foo", function () {
  describe("bar", function () {
    describe("baz", function () {
      describe("qux", function () {
        describe("this is the limit", function () {
          it("should get something", () => {
            expect(getSomething()).toBe("Something");
          });
        });
      });
    });
  });
});

配置

此规则接受一个包含以下属性的配置对象:

max

type: integer

default: 5

允许的 describe 调用最大嵌套深度。

如何使用

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

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/max-nested-describe": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["jest"],
  rules: {
    "jest/max-nested-describe": "error",
  },
});
bash
oxlint --deny jest/max-nested-describe --jest-plugin

版本

此规则添加于 v0.4.4。

参考资料