Skip to content
← Back to rules

jest/no-deprecated-functions 风格

An auto-fix is available for this rule.

它的作用

多年来,Jest 积累了部分“技术债务”,表现为一些函数被重命名以提高清晰度,或被更强大的 API 所取代。

此规则还可以自动修复其中的多项弃用项。

jest.resetModuleRegistry

自 Jest 15 起,该函数已重命名为 resetModules,并在 Jest 27 版本中被移除。

jest.addMatchers

自 Jest 17 起,该函数被 expect.extend 取代,并在 Jest 27 版本中被移除。

require.requireActualrequire.requireMock

这两个函数在 Jest 21 版本中被取代,并在 Jest 26 版本中被移除。

最初,requireActualrequireMock 函数被放置在 require 函数上。

后来,为了便于类型检查器处理,这些函数被移动到了 jest 对象上,通过 require 使用的方式也被弃用。最终,Jest 26 版本发布时,它们从 require 函数中彻底移除。

jest.runTimersToTime

该函数在 Jest 22 版本中重命名为 advanceTimersByTime,并在 Jest 27 版本中被移除。

jest.genMockFromModule

该函数在 Jest 26 版本中重命名为 createMockFromModule,并计划在 Jest 30 版本中移除。

为什么这是不好的?

尽管这些已弃用的函数通常会在代码库中保留多个主版本,但最终它们仍会被完全移除。

示例

此规则的 错误 代码示例:

javascript
jest.resetModuleRegistry; // 从 Jest 15 开始
jest.addMatchers; // 从 Jest 17 开始

配置

此规则接受一个配置对象,包含以下属性:

jest

type: object

Jest 配置选项。

jest.version

type: string

default: "29"

正在使用的 Jest 版本。

如何使用

要通过配置文件或 CLI 启用 此规则,可以使用:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/no-deprecated-functions": "error"
  }
}
bash
oxlint --deny jest/no-deprecated-functions --jest-plugin

参考资料