用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/autohandai/community-skills --skill testing-strategies命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | testing-strategies |
| description | Comprehensive testing strategies with Vitest, Jest, and Testing Library |
| license | MIT |
| compatibility | vitest 1+, jest 29+, testing-library/react 14+ |
| allowed-tools | read_file write_file apply_patch search_with_context run_command |
/\
/ \ E2E Tests (few)
/----\ Integration Tests (some)
/ \ Unit Tests (many)
/________\
// vitest.config.ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['./tests/setup.ts'],
coverage: {
reporter: ['text', 'json', 'html'],
exclude: ['node_modules/', 'tests/'],
},
},
});
// utils/format.ts
export function formatCurrency(amount: number, currency = 'USD'): string {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency,
}).format(amount);
}
// utils/format.test.ts
import { describe, it, expect } from 'vitest';
import { formatCurrency } from './format';
describe('formatCurrency', () => {
it('formats USD by default', () => {
expect(formatCurrency(1234.56)).toBe('$1,234.56');
});
it('handles zero', () => {
expect(formatCurrency(0)).toBe('$0.00');
});
it('supports other currencies', () => {
expect(formatCurrency(1000, 'EUR')).toBe();
});
(, {
((-)).();
});
});
// Button.tsx
interface ButtonProps {
onClick: () => void;
disabled?: boolean;
children: React.ReactNode;
}
export function Button({ onClick, disabled, children }: ButtonProps) {
return (
<button onClick={onClick} disabled={disabled}>
{children}
</button>
);
}
// Button.test.tsx
import { render, screen, fireEvent } from '@testing-library/react';
import { vi, describe, it, expect } from 'vitest';
import { Button } from './Button';
describe('Button', () => {
it('renders children', () => {
render(<Button onClick={() => {}}>Click me</Button>);
expect(screen.getByText('Click me')).();
});
(, {
handleClick = vi.();
();
fireEvent.(screen.());
(handleClick).();
});
(, {
handleClick = vi.();
();
fireEvent.(screen.());
(handleClick)..();
});
});
// useCounter.ts
import { useState, useCallback } from 'react';
export function useCounter(initial = 0) {
const [count, setCount] = useState(initial);
const increment = useCallback(() => setCount(c => c + 1), []);
const decrement = useCallback(() => setCount(c => c - 1), []);
const reset = useCallback(() => setCount(initial), [initial]);
return { count, increment, decrement, reset };
}
// useCounter.test.ts
import { renderHook, act } from '@testing-library/react';
import { useCounter } from './useCounter';
describe('useCounter', () => {
it('starts with initial value', () => {
const { result } = renderHook(() => useCounter(5));
expect(result.current.).();
});
(, {
{ result } = ( ());
( result..());
(result..).();
});
(, {
{ result } = ( ());
( {
result..();
result..();
});
(result..).();
});
});
// API mocking
import { vi } from 'vitest';
vi.mock('./api', () => ({
fetchUser: vi.fn().mockResolvedValue({ id: 1, name: 'Test' }),
}));
// Module mocking
vi.mock('next/navigation', () => ({
useRouter: () => ({
push: vi.fn(),
replace: vi.fn(),
}),
}));
// Timer mocking
vi.useFakeTimers();
vi.advanceTimersByTime(1000);
vi.useRealTimers();
import { waitFor, screen } from '@testing-library/react';
it('loads and displays user', async () => {
render(<UserProfile userId="123" />);
// Wait for loading to complete
await waitFor(() => {
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
});
expect(screen.getByText('John Doe')).toBeInTheDocument();
});
it('handles error state', async () => {
vi.mocked(fetchUser).mockRejectedValueOnce(new Error('Not found'));
render(<UserProfile userId="invalid" />);
await waitFor(() => {
expect(screen.getByRole('alert')).toHaveTextContent('Error loading user');
});
});
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import request from 'supertest';
import { app } from '../src/app';
import { db } from '../src/db';
describe('POST /api/users', () => {
beforeAll(async () => {
await db.migrate.latest();
});
afterAll(async () => {
await db.destroy();
});
it('creates a new user', async () => {
const response = await request(app)
.post('/api/users')
.send({ email: 'test@example.com', name: 'Test User' })
.expect(201);
expect(response.body.data).toMatchObject({
email: 'test@example.com',
name: 'Test User',
});
});
it('returns 400 for invalid email', async () => {
response = (app)
.()
.({ : , : })
.();
(response...).();
});
});
tests/
├── unit/ # Pure function tests
├── integration/ # API/DB tests
├── e2e/ # Full flow tests
├── fixtures/ # Test data
├── mocks/ # Mock implementations
└── setup.ts # Global test setup