跳至正文

jest/require-top-level-describe 样式

它的作用

如果一个测试用例 (testit) 或钩子 (beforeAllbeforeEachafterEachafterAll) 不位于顶级 describe 块中,此规则会触发一条警告。

示例

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

选项

还可以使用 maxNumberOfTopLevelDescribes 选项来强制限制顶级 describe 的数量

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

参考资料

根据 MIT 许可发行。