Skip to content
← Back to rules

jest/max-nested-describe 风格

它做了什么

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

为什么这是个问题?

过于深层地嵌套 describe() 块会使测试套件难以阅读和理解。

示例

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

错误 代码示例:

javascript
describe("foo", () => {
  describe("bar", () => {
    describe("baz", () => {
      describe("qux", () => {
        describe("quxx", () => {
          describe("太多层级", () => {
            it("应该获取某些内容", () => {
              expect(getSomething()).toBe("Something");
            });
          });
        });
      });
    });
  });
});

describe("foo", function () {
  describe("bar", function () {
    describe("baz", function () {
      describe("qux", function () {
        describe("quxx", function () {
          describe("太多层级", function () {
            it("应该获取某些内容", () => {
              expect(getSomething()).toBe("Something");
            });
          });
        });
      });
    });
  });
});

正确 代码示例:

ts
describe("foo", () => {
  describe("bar", () => {
    it("应该获取某些内容", () => {
      expect(getSomething()).toBe("Something");
    });
  });
  describe("qux", () => {
    it("应该获取某些内容", () => {
      expect(getSomething()).toBe("Something");
    });
  });
});

describe("foo2", function () {
  it("应该获取某些内容", () => {
    expect(getSomething()).toBe("Something");
  });
});

describe("foo", function () {
  describe("bar", function () {
    describe("baz", function () {
      describe("qux", function () {
        describe("这已是最大层级", function () {
          it("应该获取某些内容", () => {
            expect(getSomething()).toBe("Something");
          });
        });
      });
    });
  });
});

此规则与 eslint-plugin-vitest 兼容,要使用它,请将以下配置添加到你的 .oxlintrc.json 文件中:

json
{
  "rules": {
    "vitest/max-nested-describe": "error"
  }
}

配置

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

max

type: integer

default: 5

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

如何使用

要通过配置文件或 CLI 启用 此规则,可以使用:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/max-nested-describe": "error"
  }
}
bash
oxlint --deny jest/max-nested-describe --jest-plugin

参考资料