@antiwork/shortest

中文文档
0.4.9发布于 2025/04/01MITTypeScriptESM + CJS近 30 天下载 6.2万

AI-驱动的自然语言端到端测试框架

中文使用文档
文档目录13 节
  1. 特征
  2. 安装
  3. 快开始
  4. 使用回调函数
  5. 生命周期钩
  6. 链条测试
  7. API 测试
  8. 正在测试
  9. GitHub 2FA 登录设置
  10. 环境设置
  11. CI 设置
  12. 资源
  13. 先决条件

最短的

AI动力自然语言端到端测试框架.

特征

  • 自然语言测试书写
  • 使用 Claude 计算机使用 API 的 AI 动力测试执行
  • 建于 Playwright 上
  • GitHub 集成与 2FA 支持

安装

使用 shortest init 命令来简化新项目或现有项目中的设置进程.

这个 shortest init 命令将 :

npx @antiwork/shortest init

这将:

  • 自动安装 @antiwork/shortest 包件为 dev 依赖,如果尚未安装
  • 创建默认 shortest.config.ts 带有锅炉板配置的文件
  • 生成 a .env.local 包含需要的环境变量的占位符的文件(除非存在),例如 SHORTEST_ANTHROPIC_API_KEY 或者说 ANTHROPIC_API_KEY
  • 添加 .env.local 财务报告和审定财务报表 .shortest/ 改为 .gitignore

快开始

  1. 确定您的测试条目并在配置文件中添加您的 Anthropic API 密钥 : shortest.config.ts
import type { ShortestConfig } from "@antiwork/shortest";

export default {
  headless: false,
  baseUrl: "http://localhost:3000",
  browser: {
    contextOptions: {
      ignoreHTTPSErrors: true,
    },
  },
  testPattern: "**/*.test.ts",
  ai: {
    provider: "anthropic",
  },
} satisfies ShortestConfig;

Anthropic API 密钥默认为 SHORTEST_ANTHROPIC_API_KEY 编号 : ANTHROPIC_API_KEY 环境变量。 可以通过 ai.config.apiKey。 。 。 .

可以选择,您可以使用此选项配置浏览器行为 browser.contextOptions 属性在配置文件中。 这样您就可以通过自定义 Playwright 浏览器上下文选项。 。 。 .

  1. 使用配置中指定的模式创建测试文件 : app/login.test.ts
import { shortest } from "@antiwork/shortest";

shortest("Login to the app using email and password", {
  username: process.env.GITHUB_USERNAME,
  password: process.env.GITHUB_PASSWORD,
});

使用回调函数

也可以使用回调函数来添加附加断言和其他逻辑. AI 测试后将执行回调功能 在浏览器中执行完成.

import { shortest } from "@antiwork/shortest";
import { db } from "@/lib/db/drizzle";
import { users } from "@/lib/db/schema";
import { eq } from "drizzle-orm";

shortest("Login to the app using username and password", {
  username: process.env.USERNAME,
  password: process.env.PASSWORD,
}).after(async ({ page }) => {
  // Get current user's clerk ID from the page
  const clerkId = await page.evaluate(() => {
    return window.localStorage.getItem("clerk-user");
  });

  if (!clerkId) {
    throw new Error("User not found in database");
  }

  // Query the database
  const [user] = await db
    .select()
    .from(users)
    .where(eq(users.clerkId, clerkId))
    .limit(1);

  expect(user).toBeDefined();
});

生命周期钩

在测试前后可以使用生命周期钩来运行代码.

import { shortest } from "@antiwork/shortest";

shortest.beforeAll(async ({ page }) => {
  await clerkSetup({
    frontendApiUrl:
      process.env.PLAYWRIGHT_TEST_BASE_URL ?? "http://localhost:3000",
  });
});

shortest.beforeEach(async ({ page }) => {
  await clerk.signIn({
    page,
    signInParams: {
      strategy: "email_code",
      identifier: "[email protected]",
    },
  });
});

shortest.afterEach(async ({ page }) => {
  await page.close();
});

shortest.afterAll(async ({ page }) => {
  await clerk.signOut({ page });
});

链条测试

最短支持灵活的测试链式:

// Sequential test chain
shortest([
  "user can login with email and password",
  "user can modify their account-level refund policy",
]);

// Reusable test flows
const loginAsLawyer = "login as lawyer with valid credentials";
const loginAsContractor = "login as contractor with valid credentials";
const allAppActions = ["send invoice to company", "view invoices"];

// Combine flows with spread operator
shortest([loginAsLawyer, ...allAppActions]);
shortest([loginAsContractor, ...allAppActions]);

Shortest的风格允许设计师,营销师等非工程师,以及PM编写测试. 以下是一些例子:

shortest("visit every page and ensure no typos");
shortest("visit every page and ensure mobile layout isn't janky");
shortest("visit every page and ensure dark mode is considered");

API 测试

使用自然语言测试 API 端点

const req = new APIRequest({
  baseURL: API_BASE_URI,
});

shortest(
  "Ensure the response contains only active users",
  req.fetch({
    url: "/users",
    method: "GET",
    params: new URLSearchParams({
      active: true,
    }),
  }),
);

或者简单地说:

shortest(`
  Test the API GET endpoint ${API_BASE_URI}/users with query parameter { "active": true }
  Expect the response to contain only active users
`);

正在测试

pnpm shortest                   # Run all tests
pnpm shortest login.test.ts     # Run specific tests from a file
pnpm shortest login.test.ts:23  # Run specific test from a file using a line number
pnpm shortest --headless        # Run in headless mode using

您可以在 examples 目录.

GitHub 2FA 登录设置

目前最短版本支持使用 Github 2FA 登录. 对于 GitHub 认证测试 :

  1. 转到您的仓库设置
  2. 导航到“ Password and Authority ”
  3. 点击“ 认证器 App ”
  4. 选择“ 使用您的认证程序 ”
  5. 点击“ 设置密钥” 以获取检察官办公室的秘密
  6. 将检察官办公室的秘密添加到您的 .env.local 文件或使用最短的 CLI 来添加
  7. 将您终端中显示的 2FA 代码输入 Github 认证器设置页面, 以完成进程
shortest --github-code --secret=<OTP_SECRET>

环境设置

要求在 .env.local数字 :

ANTHROPIC_API_KEY=your_api_key
GITHUB_TOTP_SECRET=your_secret  # Only for GitHub auth tests

CI 设置

通过无头模式的测试,您可以在您的CI/CD管道中运行最短的运行. 请将您的 Anthropic API 密钥添加到您的 CI/CD 管道机密中 .

资源

先决条件

  • 反射QQ19.0.0( 如果使用 Next.js 14+ 或服务器动作)
  • Next.js QQ14.0.0(如果使用服务器组件/动作)

[尖叫声] 在 Next.js 14+ 工程中使用这个带有 React 18 的软件包可能导致与 Server Action 的类型冲突以及 useFormStatus 如果遇到形式动作或反应钩的类型错误,请确保使用反应19

版本与发布记录

@antiwork/shortest 共发布 23 个版本,首次发布于 2024/11/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.9latest2025/04/011.6 年前
0.4.82025/03/251.6 年前
0.4.72025/03/171.6 年前
0.4.62025/03/091.7 年前
0.4.52025/03/021.7 年前
0.4.42025/02/231.7 年前

关于 @antiwork/shortest 的常见问题

@antiwork/shortest 是什么?

AI-驱动的自然语言端到端测试框架

如何安装 @antiwork/shortest?

在项目目录运行 npm install @antiwork/shortest,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@ai-sdk/anthropic(^1.1.15)、@ai-sdk/provider(^1.0.10)、ai(^4.1.53)、dotenv(^16.4.5)、esbuild(^0.20.1)、expect(^29.7.0)、mailosaur(^8.7.0)、playwright(^1.50.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 shortest,可以用 npx shortest 直接运行。

@antiwork/shortest 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@antiwork/shortest 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@antiwork/shortest 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@antiwork/shortest 可以商用吗?

@antiwork/shortest 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@antiwork/shortest 的下载量有多少?

根据 npm 官方下载统计,@antiwork/shortest 在 2026/10/06 – 2026/11/04 这 30 天内被下载 62,355 次,日均约 2,079 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@antiwork/shortest 还在维护吗?

最新版本 0.4.9 发布于 2025/04/01(1.6 年前);至今共发布 23 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/11/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。