Skip to content
← Back to rules

vitest/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": ["vitest"],
  "rules": {
    "vitest/max-nested-describe": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则于 v0.4.4 中加入。

参考资料