AgentSkillsCN

Testing

测试

SKILL.md

Skill: Testing

Description

Generates and runs tests following project testing strategy.

When to Use

  • After implementing a feature
  • When user triggers /test workflow
  • During code review

Instructions

Test Structure

  • Co-locate tests: Component.tsxComponent.test.tsx
  • Use descriptive test names: it('should redirect unauthenticated users to login')
  • Follow Arrange-Act-Assert pattern

Unit Tests (Vitest/Jest)

typescript
import { describe, it, expect } from 'vitest';
import { functionUnderTest } from './module';

describe('functionUnderTest', () => {
  it('should handle normal case', () => {
    // Arrange
    const input = 'test';
    // Act
    const result = functionUnderTest(input);
    // Assert
    expect(result).toBe('expected');
  });

  it('should handle edge case', () => {
    expect(functionUnderTest('')).toBeNull();
  });

  it('should throw on invalid input', () => {
    expect(() => functionUnderTest(null)).toThrow();
  });
});

E2E Tests (Playwright)

typescript
import { test, expect } from '@playwright/test';

test('user can log in and see dashboard', async ({ page }) => {
  await page.goto('/login');
  await page.fill('[name=email]', 'test@example.com');
  await page.fill('[name=password]', 'password123');
  await page.click('button[type=submit]');
  await expect(page).toHaveURL('/dashboard');
  await expect(page.getByRole('heading')).toContainText('Dashboard');
});

What to Test

  • Always: Business logic, utilities, API handlers
  • Usually: Component rendering, user interactions
  • Sometimes: Integration between services
  • Never: Third-party library internals, CSS styling