AgentSkillsCN

testing

LMS Next.js/Convex 项目的完整测试指南,采用四层策略,覆盖 Convex 后端测试(convex-test)、React 组件测试(Testing Library)、集成工作流测试,以及 E2E 测试(Playwright)。当您在 `**/*.test.ts`、`**/*.spec.ts`、`tests/**` 等文件中进行开发,或被要求编写测试、测试组件、验证覆盖率、实施 TDD 时,可使用此技能。关键词触发:vitest、playwright、testing library、convex-test、coverage、TDD、单元测试、E2E 测试、集成测试。

SKILL.md
--- frontmatter
name: testing
description: Complete testing guide for the LMS project with a 4-layer strategy covering Convex backend tests (convex-test), React component tests (Testing Library), integration workflow tests, and E2E tests (Playwright). Use when working on files matching `**/*.test.ts`, `**/*.spec.ts`, `tests/**`, or when asked to write tests, test a component, verify coverage, implement TDD. Triggers on keywords like vitest, playwright, testing library, convex-test, coverage, TDD, unit test, e2e test, integration test.

Testing Skill - LMS Project

Quick Start

bash
# Run all unit tests
pnpm test

# Run tests in watch mode
pnpm test:watch

# Run E2E tests
pnpm test:e2e

# Run E2E tests with UI
pnpm test:e2e:ui

# Generate coverage report (target: 80%)
pnpm test:coverage

Decision Tree - Which Test Layer?

code
Is it a Convex function (query/mutation/action)?
├─ YES → Use convex-test (see references/convex-testing.md)
│
└─ NO → Is it a React component in isolation?
         ├─ YES → Use Testing Library (see references/rtl-patterns.md)
         │
         └─ NO → Does it involve multiple components/services?
                  ├─ YES (same process) → Integration test with Vitest
                  │
                  └─ YES (real browser needed) → Playwright E2E (see references/playwright.md)

Test Layer Overview

LayerToolLocationExtensionUse Case
Unit (Convex)convex-testconvex/*.test.ts.test.tsQueries, mutations, actions
Unit (React)Testing Librarytests/unit/.test.tsComponents in isolation
IntegrationVitesttests/unit/.test.tsMulti-component workflows
E2EPlaywrighttests/e2e/.spec.tsFull user journeys

Core Conventions

File Organization

  • Unit tests: tests/unit/ with .test.ts extension
  • E2E tests: tests/e2e/ with .spec.ts extension
  • Convex tests: convex/*.test.ts alongside source files

Pattern AAA (Arrange, Act, Assert)

typescript
it("should do something", async () => {
  // Arrange - Setup test data and conditions
  const user = userEvent.setup();
  render(<Component />);

  // Act - Perform the action being tested
  await user.click(screen.getByRole("button", { name: /submit/i }));

  // Assert - Verify the expected outcome
  expect(screen.getByText("Success")).toBeInTheDocument();
});

Query Priority (Testing Library)

  1. getByRole - Accessible queries (PREFERRED)
  2. getByLabelText - Form fields
  3. getByPlaceholderText - Input hints
  4. getByText - Text content
  5. getByTestId - Last resort only

User Interactions

Always use userEvent over fireEvent:

typescript
// ✅ Correct
const user = userEvent.setup();
await user.click(button);
await user.type(input, "text");

// ❌ Avoid
fireEvent.click(button);

Technology Stack

PackageVersionPurpose
vitest3.2.4Test runner (globals: true, jsdom)
@testing-library/react16.3.0React component testing
@testing-library/jest-dom6.9.1DOM matchers
@testing-library/user-event14.xUser interaction simulation
convex-test0.0.41Convex backend testing
@playwright/test1.57.0E2E browser testing

Coverage Requirements

Target: 80% minimum on all metrics (statements, branches, functions, lines).

bash
# Check coverage
pnpm test:coverage

# Coverage output locations
# - Terminal: text summary
# - coverage/index.html: detailed HTML report
# - coverage/coverage-final.json: JSON for CI

Detailed References