For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/guide/start/quick-start.md.
close
  • 简体中文
  • 快速上手

    在线示例

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

    环境准备

    开始之前,需要先安装 Node.js >= 20.19.0 版本,推荐使用 Node.js LTS 版本。

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

    node -v

    如果你的环境中尚未安装 Node.js,或是版本太低,可以通过 nvm 或 fnm 安装。

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

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

    使用 Rstest

    Agent prompt

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

    For your Agent
    设置 Rstest

    复制这个 prompt 并发送给你的 Coding Agent。

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

    For your Agent
    迁移到 Rstest

    复制这个 prompt 并发送给你的 Coding Agent。

    手动安装

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

    npm
    yarn
    pnpm
    bun
    deno
    npm add @rstest/core -D

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

    package.json
    {
      "scripts": {
        "test": "rstest"
      }
    }

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

    Rstest 内置了 watch、run 等命令,请参考 CLI 工具 来了解所有可用命令以及选项。

    编写测试

    作为一个简单的例子,我们有一个 sayHi 方法。为了对它进行测试,你可以创建一个名为 index.test.ts 的测试文件或使用与 Rust 测试 类似的 In-Source 测试。

    index.ts
    export const sayHi = () => 'hi';
    index.test.ts
    import { expect, test } from '@rstest/core';
    import { sayHi } from '../src/index';
    
    test('should sayHi correctly', () => {
      expect(sayHi()).toBe('hi');
    });

    接下来,你可以通过 使用 Rstest 中配置好的命令执行测试。Rstest 会打印如下内容:

     ✓ test/index.test.ts (1)
    
     Test Files 1 passed
          Tests 1 passed
       Duration 140 ms (build 17 ms, tests 123 ms)

    下一步

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

    • node:Node.js 测试与覆盖率收集。
    • react:使用 happy-dom 的 React 组件、Hook 和 SSR 测试。
    • vue:Vue 组件与 SSR 测试。
    • browser-react:Browser Mode 下的 React 组件测试。

    更多示例请参考 Rstest examples。