> For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt.

# globals

- **类型：** `boolean`
- **默认值：** `false`
- **CLI：** `--globals`

为测试文件提供全局的 Rstest API，如 `expect`、`test`、`describe` 等。

默认情况下，Rstest 不提供全局 API。如果你想像使用 Jest 一样全局使用这些 API，可以在配置中添加 `globals: true`或者在命令行中传入 `--globals` 选项。


**CLI**

```bash
npx rstest --globals
```


**rstest.config.ts**

```ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  globals: true,
});
```


## 使用

当你启用 `globals` 后，就可以直接使用全局 API，而无需 `import { ... } from '@rstest/core'`。

```diff title="index.test.ts"
- import { describe, expect, it } from '@rstest/core';

describe('Index', () => {
  it('should add two numbers correctly', () => {
    expect(1 + 1).toBe(2);
  });
});
```

## 可用的全局 API

启用 `globals` 后，Rstest 会注入以下全局 API：

- `test`
- `describe`
- `it`
- `expect`
- `assert`
- `beforeAll`
- `afterAll`
- `beforeEach`
- `afterEach`
- `onTestFinished`
- `onTestFailed`
- `rs`
- `rstest`

`rs` 和 `rstest` 是等价别名，提供相同的 runtime utility。

### 类型支持

为了让 TypeScript 正确识别全局 API，推荐在测试文件对应的 `tsconfig.json` 中，将 `@rstest/core/globals` 添加到 `compilerOptions.types`：

```json title=tsconfig.json
{
  "compilerOptions": {
    "types": ["@rstest/core/globals"]
  }
}
```

> 如果已有 `compilerOptions.types` 配置，请将该类型追加到现有列表中。

或者创建一个 `src/rstestEnv.d.ts` 文件来引用类型定义：

```ts title=rstestEnv.d.ts
/// <reference types="@rstest/core/globals" />
```
