← Back to list

testing-best-practices
by hydershah
Advance Appliance - Next.js 15 + Payload CMS website with 3 design themes
⭐ 0🍴 0📅 Jan 25, 2026
SKILL.md
name: testing-best-practices description: Testing methodologies, patterns, and best practices for unit, integration, and E2E tests. (project) allowed-tools: Read, Grep, Glob, Edit, Write, Bash
Testing Best Practices
Testing Pyramid
/\
/E2E\ <- Few, slow, expensive
/------\
/Integration\ <- Some, moderate speed
/--------------\
/ Unit Tests \ <- Many, fast, cheap
/------------------\
Unit Testing
AAA Pattern
describe('Calculator', () => {
it('should add two numbers correctly', () => {
// Arrange
const calculator = new Calculator();
// Act
const result = calculator.add(2, 3);
// Assert
expect(result).toBe(5);
});
});
Naming Convention
// Pattern: should [expected behavior] when [condition]
it('should throw error when dividing by zero')
it('should return empty array when no items match')
it('should update user when valid data provided')
Test Organization
describe('UserService', () => {
describe('createUser', () => {
it('should create user with valid data')
it('should throw error when email exists')
it('should hash password before saving')
});
describe('deleteUser', () => {
it('should remove user from database')
it('should throw error when user not found')
});
});
Mocking
Function Mocks
const mockFn = jest.fn();
mockFn.mockReturnValue('value');
mockFn.mockResolvedValue('async value');
mockFn.mockRejectedValue(new Error('fail'));
Module Mocks
jest.mock('./database', () => ({
query: jest.fn().mockResolvedValue([]),
}));
Spy on Methods
const spy = jest.spyOn(service, 'method');
expect(spy).toHaveBeenCalledWith(arg1, arg2);
Integration Testing
API Testing
describe('POST /api/users', () => {
it('should create a new user', async () => {
const response = await request(app)
.post('/api/users')
.send({ name: 'John', email: 'john@example.com' })
.expect(201);
expect(response.body.data.name).toBe('John');
});
});
Database Testing
beforeEach(async () => {
await db.migrate.latest();
await db.seed.run();
});
afterEach(async () => {
await db.migrate.rollback();
});
E2E Testing
Playwright Example
test('user can login', async ({ page }) => {
await page.goto('/login');
await page.fill('[name="email"]', 'user@example.com');
await page.fill('[name="password"]', 'password123');
await page.click('button[type="submit"]');
await expect(page).toHaveURL('/dashboard');
});
Code Coverage
Coverage Targets
- Statements: 80%+
- Branches: 75%+
- Functions: 80%+
- Lines: 80%+
Focus Areas
- Critical business logic
- Edge cases and error handling
- Security-sensitive code
Don't Over-Test
- Getters/setters
- Framework code
- Third-party libraries
- Simple transformations
Test Data
Factories
const createUser = (overrides = {}) => ({
id: faker.string.uuid(),
name: faker.person.fullName(),
email: faker.internet.email(),
...overrides,
});
Fixtures
// fixtures/users.json
{
"admin": { "id": "1", "role": "admin" },
"user": { "id": "2", "role": "user" }
}
Score
Total Score
40/100
Based on repository quality metrics
✓SKILL.md
SKILL.mdファイルが含まれている
+20
○LICENSE
ライセンスが設定されている
0/10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
Reviews
💬
Reviews coming soon