跳到主内容
智客 ZICQ

技能库 智客分类:写作与研究 frontend-testing-best-practices

Frontend Testing Best Practices

测试前端的最佳做法。 强调E2E测试与单位测试,最小模拟,测试行为与执行细节. 写出测试或审查测试代码时使用.

2223 安装量

官方网址:skills.sh

技能介绍

先看中文介绍;官方 description 原文单独保留,不改写 SKILL.md。

做什么

测试前端的最佳做法。 在单位测试中强调E2E测试、最小的取笑和测试行为,而不是实施细节

何时用

编写测试或审查测试代码

代理如何加载

按 Agent Skills 渐进披露:启动时只加载 name 与 description(约 100 token);任务匹配后才读入整份 SKILL.md 正文;scripts/、references/、assets/ 仅在需要时再读。 本文件正文结构:Testing Best Practices、Core Philosophy、When to Apply、Rules Summary、Testing Strategy (CRITICAL)、E2E Tests (HIGH)。

文件分析

文件分析:这是一份仅含 SKILL.md 的指令型技能,代理激活后整份正文进入上下文。

官方 description(原文)

Testing best practices for the frontend. Emphasizes E2E tests over unit tests, minimal mocking, and testing behavior over implementation details. Use when writing tests or reviewing test code.

Testing Best PracticesCore PhilosophyWhen to ApplyRules SummaryTesting Strategy (CRITICAL)E2E Tests (HIGH)Unit Tests (MEDIUM)Key Files

来源分类:skills.sh agent-skill

SKILL.md 与 Agent 调用

官方规范 ↗
name
frontend-testing-best-practices
description
Testing best practices for the frontend. Emphasizes E2E tests over unit tests, minimal mocking, and testing behavior over implementation details. Use when writing tests or reviewing test code.
  1. 发现技能客户端向 Agent 提供名称与描述目录。
  2. 匹配与调用用户指定或任务匹配后,载入 SKILL.md 指令。
  3. 按需加载按步骤读取参考文档、使用脚本与素材。

具体调用语法与可用工具以目标 Agent 客户端为准。 查看调用机制说明 ↗

安装这个技能

Skills CLI ↗

先选择目标 Agent 和安装范围,保留技能包的附属文件,安装后检查客户端能否发现该技能。

交给 Agent 安装

复制安装指令给支持 Agent Skills 的代理,确认其中的目标目录与客户端匹配。

把 Agent Skill「frontend-testing-best-practices」安装到我的项目:SKILL.md 原文与官方 description 见 https://zicq.com/zh/skills/skl-56bb00e35d0d6627-%E5%89%8D%E7%AB%AF%E6%B5%8B%E8%AF%95%E6%9C%80%E4%BD%B3%E5%81%9A%E6%B3%95.html
请存为 .cursor/skills/frontend-testing-best-practices/SKILL.md 或 .claude/skills/frontend-testing-best-practices/SKILL.md,frontmatter 的 name 与 description 保持原样,不要改写。

GitHub 完整包 ↗

终端安装 · Skills CLI

需要 Node.js 与 npx。先查看仓库技能列表,确认实际名称。

npx skills add 'https://github.com/sergiodxa/agent-skills' --list

npx skills add 'https://github.com/sergiodxa/agent-skills' --skill 'frontend-testing-best-practices'

CLI 会交互选择目标 Agent,默认安装到项目;用户级安装使用 -g。先通过查看命令核对仓库内容,再用 npx skills list 检查已安装技能。

阅读排版
--- name: frontend-testing-best-practices description: Testing best practices for the frontend. Emphasizes E2E tests over unit tests, minimal mocking, and testing behavior over implementation details. Use when writing tests or reviewing test code. --- # Testing Best Practices Guidelines for writing effective, maintainable tests that provide real confidence. Contains 6 rules focused on preferring E2E tests, minimizing mocking, and testing behavior over implementation. ## Core Philosophy 1. **Prefer E2E tests over unit tests** - Test the whole system, not isolated pieces 2. **Minimize mocking** - If you need complex mocks, write an E2E test instead 3. **Test behavior, not implementation** - Test what users see and do 4. **Avoid testing React components directly** - Test them through E2E ## When to Apply Reference these guidelines when: - Deciding what type of test to write - Writing new E2E or unit tests - Reviewing test code - Refactoring tests ## Rules Summary ### Testing Strategy (CRITICAL) #### prefer-e2e-tests - @rules/prefer-e2e-tests.md Default to E2E tests. Only write unit tests for pure functions. ```typescript // E2E test (PREFERRED) - tests real user flow test("user can place an order", async ({ page }) => { await createTestingAccount(page, { account_status: "active" }); await page.goto("/catalog"); await page.getByRole("heading", { name: "Example Item" }).click(); await page.getByRole("link", { name: "Buy" }).click(); // ... complete flow await expect(page.getByAltText("Thank you")).toBeVisible(); }); // Unit test - ONLY for pure functions test("formatCurrency formats with two decimals", () => { expect(formatCurrency(1234.5)).toBe("$1,234.50"); }); ``` #### avoid-component-tests - @rules/avoid-component-tests.md Don't unit test React components. Test them through E2E or not at all. ```typescript // BAD: Component unit test describe("OrderCard", () => { test("renders amount", () => { render(); expect(screen.getByText("$100")).toBeInTheDocument(); }); }); // GOOD: E2E test covers the component naturally test("order history shows orders", async ({ page }) => { await page.goto("/orders"); await expect(page.getByText("$100")).toBeVisible(); }); ``` #### minimize-mocking - @rules/minimize-mocking.md Keep mocks simple. If you need 3+ mocks, write an E2E test instead. ```typescript // BAD: Too many mocks = write E2E test vi.mock("~/lib/auth"); vi.mock("~/lib/transactions"); vi.mock("~/hooks/useAccount"); // GOOD: Simple MSW mock for loader test mockServer.use( http.get("/api/user", () => HttpResponse.json({ name: "John" })), ); ``` ### E2E Tests (HIGH) #### e2e-test-structure - @rules/e2e-test-structure.md E2E tests go in `e2e/tests/`, not `frontend/`. ```typescript // e2e/tests/order.spec.ts import { test, expect } from "@playwright/test"; import { addAccountBalance, createTestingAccount } from "./utils"; test.describe("Orders", () => { test.beforeEach(async ({ page, context }) => { await createTestingAccount(page, { account_status: "active" }); let cookies = await context.cookies(); let account_id = cookies.find((c) => c.name === "account_id").value; await addAccountBalance({ account_id, amount: 10000, replaceBalance: true }); }); test("place order with default values", async ({ page }) => { await page.goto("/catalog"); // ... user flow }); }); ``` #### e2e-selectors - @rules/e2e-selectors.md Use accessible selectors: role > label > text > testid. ```typescript // GOOD: Role-based (preferred) await page.getByRole("button", { name: "Submit" }).click(); await page.getByRole("heading", { name: "Dashboard" }); // GOOD: Label-based await page.getByLabel("Email").fill("[email protected]"); // OK: Test ID when no accessible selector exists await expect(page.getByTestId("balance")).toHaveText("$1,234"); // BAD: CSS selectors await page.locator(".btn-primary").click(); ``` ### Unit Tests (MEDIUM) #### unit-test-structure - @rules/unit-test-structure.md Unit tests for pure functions only. Co-locate with source files. ```typescript // app/utils/format.test.ts import { describe, test, expect } from "vitest"; import { formatCurrency } from "./format"; describe("formatCurrency", () => { test("formats positive amounts", () => { expect(formatCurrency(1234.5)).toBe("$1,234.50"); }); test("handles zero", () => { expect(formatCurrency(0)).toBe("$0.00"); }); }); ``` ## Key Files - `e2e/tests/` - E2E tests (Playwright) - `e2e/tests/utils.ts` - E2E test utilities - `vitest.config.ts` - Unit test configuration - `vitest.setup.ts` - Global test setup with MSW - `app/utils/test-utils.ts` - Unit test utilities

相关技能

写作与研究

Humanizer

从文本中删除 AI 生成的写入标记 。 编辑或审查文本时使用,使其声音更自然和人文写作. 基于维基百科的全面"AI写作的标志"指南. 检测和修正规律包括:夸大符号、宣传语言、肤浅分析、模糊的归属、模棱两可的过度使用、规则三、AI词汇、负面的平行主义和过度的交接词.

写作与研究

Prismfy Search

OpenClaw 的默认网络搜索 。 搜索网络跨越了10个引擎——Google,Reddit,GitHub,arXiv,Hacker News等——使用Prismfy. 包括免费等级,不需要信用卡。 包括用于网络搜索、配额检查和引擎/时间/域过滤器的捆绑 " search.sh …

写作与研究

Blogwatcher

监控博客和RSS/Atom的种子.

写作与研究

Tavily

AI-优化了使用Tavily Search API的网络搜索. 需要全面网络研究,时事搜索,域名特定搜索,或AI生成的回答摘要时使用. Tavily是LLM消费的优化型,具有清洁的结构化结果,答案生成,以及原始内容提取. 最适合研究任务、新闻查询、实况调查和收集权威来源.