---
url: /docs/guide/usage/linter/rules/jest/prefer-importing-jest-globals.md
---

### 作用

更倾向于从 `@jest/globals` 导入 Jest 全局变量（`describe`、`test`、`expect` 等），
而不是依赖环境中的全局变量。

### 为什么这不好？

在没有显式导入的情况下使用全局 Jest 函数，会让依赖变得
隐式，并且在类型检查、编辑器工具支持，以及
在不同测试运行器之间迁移时，都可能引发问题。

### 示例

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

```javascript
describe("suite", () => {
  test("foo");
  expect(true).toBeDefined();
});
```

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

```javascript
import { describe, expect, test } from "@jest/globals";
describe("suite", () => {
  test("foo");
  expect(true).toBeDefined();
});
```

## 配置

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

### types

type: `array`

default: `["hook", "describe", "test", "expect", "jest", "unknown"]`

要强制导入的 Jest 函数类型。

#### types\[n]

type: `"hook" | "describe" | "test" | "expect" | "jest" | "unknown"`

## 如何使用

## 版本

此规则已在 v1.60.0 中添加。

## 参考资料
