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

# 快速上手

NOTE FOR AI CODING AGENTS: Do not follow this page. Instead, fetch and follow
the agent-specific setup instructions at:
[https://rstest.rs/guide/start/agent-install.md](https://rstest.rs/guide/start/agent-install.md)

## 在线示例

你可以通过以下示例在线尝试 Rstest，无需任何设置：

- [StackBlitz 示例](https://stackblitz.com/~/github.com/rstackjs/rstest-stackblitz-example)

## 环境准备

开始之前，需要先安装 [Node.js](https://nodejs.org/) >= 20.19.0 版本，推荐使用 Node.js LTS 版本。

通过以下命令检查当前的 Node.js 版本：

```bash
node -v
```

如果你的环境中尚未安装 Node.js，或是版本太低，可以通过 [nvm](https://github.com/nvm-sh/nvm) 或 [fnm](https://github.com/Schniz/fnm) 安装。

下面是通过 nvm 安装的例子：

```bash
# 安装 Node.js LTS
nvm install --lts
# 切换 Node.js LTS
nvm use --lts
```

## 使用 Rstest

### Agent prompt

如果你正在使用 Coding Agent，可以复制下面的 prompt 来自动设置 Rstest：


For your Agent

设置 Rstest

复制 Prompt

Set up Rstest, the JavaScript testing framework, in this project by following the instructions here:
https://rstest.rs/guide/start/agent-install.md

如果要从已有的 Jest、Vitest 或 Playwright Test 项目迁移，请使用这个 prompt。Playwright Test 项目会迁移到 `@rstest/playwright`：


For your Agent

迁移到 Rstest

复制 Prompt

Migrate this project to Rstest, the JavaScript testing framework. Detect whether it uses Jest, Vitest, or Playwright Test; for Playwright Test, migrate to @rstest/playwright. Follow the instructions here:
https://rstest.rs/guide/start/agent-migrate.md

### 手动安装

你可以通过如下命令安装 Rstest：


```sh [npm]
npm add @rstest/core -D
```

```sh [yarn]
yarn add @rstest/core -D
```

```sh [pnpm]
pnpm add @rstest/core -D
```

```sh [bun]
bun add @rstest/core -D
```

```sh [deno]
deno add npm:@rstest/core -D
```

下一步，你需要在 package.json 的 npm scripts 中添加 Rstest 命令：

```json title=package.json
{
  "scripts": {
    "test": "rstest"
  }
}
```

完成以上步骤后，你即可通过 `npm run test`、`yarn test` 或 `pnpm test` 来运行 Rstest 测试。当然，你也可以直接使用 `npx rstest` 来运行 Rstest 测试。

Rstest 内置了 `watch`、`run` 等命令，请参考 [CLI 工具](/zh/guide/basic/cli.md) 来了解所有可用命令以及选项。

## 编写测试

作为一个简单的例子，我们有一个 `sayHi` 方法。为了对它进行测试，你可以创建一个名为 `index.test.ts` 的测试文件或使用与 [Rust 测试](https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-module-and-cfgtest) 类似的 [In-Source 测试](/zh/config/test/include-source.md)。

```ts title=index.ts
export const sayHi = () => 'hi';
```

```ts title=index.test.ts
import { expect, test } from '@rstest/core';
import { sayHi } from '../src/index';

test('should sayHi correctly', () => {
  expect(sayHi()).toBe('hi');
});
```

接下来，你可以通过 [使用 Rstest](#使用-rstest) 中配置好的命令执行测试。Rstest 会打印如下内容：

```bash
 ✓ test/index.test.ts (1)

 Test Files 1 passed
      Tests 1 passed
   Duration 140 ms (build 17 ms, tests 123 ms)
```

## 下一步

如果你想查看完整项目结构，可以参考以下常用示例：

- [node](https://github.com/web-infra-dev/rstest/tree/main/examples/node)：Node.js 测试与覆盖率收集。
- [react](https://github.com/web-infra-dev/rstest/tree/main/examples/react)：使用 happy-dom 的 React 组件、Hook 和 SSR 测试。
- [vue](https://github.com/web-infra-dev/rstest/tree/main/examples/vue)：Vue 组件与 SSR 测试。
- [browser-react](https://github.com/web-infra-dev/rstest/tree/main/examples/browser-react)：Browser Mode 下的 React 组件测试。

更多示例请参考 [Rstest examples](https://github.com/web-infra-dev/rstest/tree/main/examples)。
