jest/no-duplicate-hooks Style
它的作用
禁止在 describe 块中出现重复的 hooks。
为什么这不好?
在 describe 块中存在重复的 hooks 可能会导致混乱和意外行为。 当存在多个同类型的 hooks 时,它们都会按顺序执行,这会使得 测试设置流程难以理解,并可能导致冗余或冲突的操作。这会让测试 更难维护和调试。
示例
以下是此规则的错误代码示例:
javascript
describe("foo", () => {
beforeEach(() => {
// 一些设置
});
beforeEach(() => {
// 一些设置
});
test("foo_test", () => {
// 一些测试
});
});
// 嵌套 describe 场景
describe("foo", () => {
beforeEach(() => {
// 一些设置
});
test("foo_test", () => {
// 一些测试
});
describe("bar", () => {
test("bar_test", () => {
afterAll(() => {
// 一些清理
});
afterAll(() => {
// 一些清理
});
});
});
});以下是此规则的正确代码示例:
javascript
describe("foo", () => {
beforeEach(() => {
// 一些设置
});
test("foo_test", () => {
// 一些测试
});
});
// 嵌套 describe 场景
describe("foo", () => {
beforeEach(() => {
// 一些设置
});
test("foo_test", () => {
// 一些测试
});
describe("bar", () => {
test("bar_test", () => {
beforeEach(() => {
// 一些设置
});
});
});
});如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["jest"],
"rules": {
"jest/no-duplicate-hooks": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["jest"],
rules: {
"jest/no-duplicate-hooks": "error",
},
});bash
oxlint --deny jest/no-duplicate-hooks --jest-plugin版本
此规则于 v0.4.0 中添加。
