| name | testing |
| description | Write and run tests including unit tests, integration tests, and E2E tests. Use when ensuring code quality, writing test cases, or setting up testing frameworks. |
🧪 Testing Skill
Test Types
| Type | Purpose | Tools |
|---|
| Unit | Test functions in isolation | Jest, Vitest, pytest |
| Integration | Test modules together | Supertest, pytest |
| E2E | Test full user flows | Playwright, Cypress |
Unit Test Patterns
JavaScript/TypeScript (Jest/Vitest)
describe('calculateTotal', () => {
it('should add tax correctly', () => {
expect(calculateTotal(100, 0.07)).toBe(107);
});
it('should handle zero price', () => {
expect(calculateTotal(0, 0.07)).toBe(0);
});
it('should throw on negative price', () => {
expect(() => calculateTotal(-10, 0.07)).toThrow();
});
});
Python (pytest)
def test_calculate_total():
assert calculate_total(100, 0.07) == 107
def test_calculate_total_zero():
assert calculate_total(0, 0.07) == 0
def test_calculate_total_negative():
with pytest.raises(ValueError):
calculate_total(-10, 0.07)
Test Structure (AAA Pattern)
test('should upload file successfully', async () => {
const file = createMockFile('video.mp4');
const uploader = new Uploader();
const result = await uploader.upload(file);
expect(result.success).toBe(true);
expect(result.url).toContain('uploaded');
});
Mocking
const mockFetch = jest.fn().mockResolvedValue({ ok: true });
jest.mock('./api', () => ({
fetchData: jest.fn().mockResolvedValue({ data: [] })
}));
jest.useFakeTimers();
jest.advanceTimersByTime(3000);
E2E Testing (Playwright)
test('upload flow', async ({ page }) => {
await page.goto('https://tiktok.com/upload');
await page.setInputFiles('input[type="file"]', 'video.mp4');
await page.fill('[placeholder="Title"]', 'My Video');
await page.click('button:has-text("Post")');
await expect(page.locator('.success')).toBeVisible();
});
Test Checklist
🤖 AI Test Generation
Auto-Generate Test Cases
function validateEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
describe('validateEmail', () => {
test('valid email returns true', () => {
expect(validateEmail('user@example.com')).toBe(true);
});
test.each([
['', false],
['user', false],
['user@', false],
['@example.com', false],
['user@example', false],
['user.name@example.co.th', true],
])('validateEmail(%s) = %s', (input, expected) => {
expect(validateEmail(input)).toBe(expected);
});
});
📊 Coverage Analysis
Commands
npm test -- --coverage
npx vitest --coverage
npx nyc npm test
Coverage Targets
| Metric | Minimum | Good |
|---|
| Statements | 70% | 90%+ |
| Branches | 70% | 85%+ |
| Functions | 70% | 90%+ |
| Lines | 70% | 90%+ |
📸 Snapshot Testing
test('component renders correctly', () => {
const tree = render(<Button label="Click me" />);
expect(tree).toMatchSnapshot();
});
Test Commands
npm test
npm test -- button.test.js
npm test -- --watch
npm test -- -u
npm test -- --coverage --coverageReporters=text