Skip to content
← Back to rules

vitest/no-conditional-tests Correctness

作用

该规则禁止在测试用例中使用条件语句,以确保测试具有确定性并且易于阅读。

为什么这不好?

测试用例中的条件语句会使测试变得不可预测,并且更难理解。测试应当保持一致且直接,以确保结果可靠并便于维护。

示例

以下是此规则的错误代码示例:

js
describe("my tests", () => {
  if (true) {
    it("is awesome", () => {
      doTheThing();
    });
  }
});

以下是此规则的正确代码示例:

js
describe("my tests", () => {
  it("is awesome", () => {
    doTheThing();
  });
});

如何使用

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

json
{
  "plugins": ["vitest"],
  "rules": {
    "vitest/no-conditional-tests": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vitest"],
  rules: {
    "vitest/no-conditional-tests": "error",
  },
});
bash
oxlint --deny vitest/no-conditional-tests --vitest-plugin

版本

此规则于 v0.8.0 中添加。

参考资料