Skip to content
← Back to rules

jest/no-alias-methods Style

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

它的作用

强制使用 Jest 的规范 matcher 名称,而不是别名。

为什么这不好?

Jest matcher 别名已被弃用,并将在 Jest 的下一个大版本中移除。 更多信息请参见 jestjs/jest#13164

此规则使查找某个 matcher 的所有出现位置更容易,并确保 matcher 名称之间保持一致。

示例

此规则的错误代码示例:

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": ["jest"],
  "rules": {
    "jest/no-alias-methods": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则于 v0.0.12 中添加。

参考资料