Skip to content
← Back to rules

vitest/no-importing-vitest-globals Style

🛠️ An auto-fix is available for this rule.

作用

该规则禁止导入任何 vitest 全局函数。

为什么这很糟糕?

如果一个项目已配置为将 Vitest 函数作为全局变量提供, 则可以使用此规则来确保这些全局变量不会通过 importrequire 被导入。

请注意,如果 Vitest 中的 globals 配置 选项设置为 false,则不应使用此规则(false 是默认配置)。

示例

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

js
import { test, expect } from "vitest";

test("foo", () => {
  expect(1).toBe(1);
});
js
const { test, expect } = require("vitest");

test("foo", () => {
  expect(1).toBe(1);
});

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

js
test("foo", () => {
  expect(1).toBe(1);
});

如何使用

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

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

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

版本

该规则于 v1.49.0 中新增。

参考资料