Skip to content
← Back to rules

vitest/no-alias-methods Style

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

作用

强制使用 Vitest 的标准匹配器名称,而不是别名。

为什么这很糟糕?

不建议使用 Vitest 匹配器别名,因为它们会导致匹配器用法不一致。 此规则便于搜索某个匹配器的所有出现位置,并确保匹配器名称保持一致。

示例

此规则的错误代码示例:

javascript
expect(a).toBeCalled();
expect(a).toBeCalledTimes();
expect(a).toBeCalledWith();
expect(a).lastCalledWith();
expect(a).nthCalledWith();
expect(a).toReturn();
expect(a).toReturnTimes();
expect(a).toReturnWith();
expect(a).lastReturnedWith();
expect(a).nthReturnedWith();
expect(a).toThrowError();

此规则的正确代码示例:

javascript
expect(a).toHaveBeenCalled();
expect(a).toHaveBeenCalledTimes();
expect(a).toHaveBeenCalledWith();
expect(a).toHaveBeenLastCalledWith();
expect(a).toHaveBeenNthCalledWith();
expect(a).toHaveReturned();
expect(a).toHaveReturnedTimes();
expect(a).toHaveReturnedWith();
expect(a).toHaveLastReturnedWith();
expect(a).toHaveNthReturnedWith();
expect(a).toThrow();

How to use

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

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

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

版本

此规则添加于 v0.0.12。

参考资料