بنقرة واحدة
code-testing
Use when writing tests, reviewing test quality, debugging flaky tests, or deciding what/how to test.
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Use when writing tests, reviewing test quality, debugging flaky tests, or deciding what/how to test.
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Use when producing product artifacts - epics, user stories, acceptance criteria, or analytics specs.
Use when defining product direction, vision document sections, or roadmap. Triggers on strategic product planning, vision creation, or vision document quality review.
Use when designing UI, choosing colors, typography, spacing, creating visual hierarchy, or making design decisions. Based on Refactoring UI by Wathan & Schoger.
Use when someone needs a plain-language explanation of how an existing feature or edge case behaves in the codebase. Triggers - non-technical stakeholder asks how something works, need code-informed functional explanation, need to understand rules or edge cases without implementation planning.
Use when a developer wants to go from rough idea to working code autonomously. Triggers - imperfect prompt, solo dev wants full pipeline, "just build it", idea to implementation without manual steps.
Use when creating a new agentic workflow skill. Triggers - need new multi-step orchestrated process, want to add workflow to agentic framework, building spec-driven pipeline.
| name | code-testing |
| description | Use when writing tests, reviewing test quality, debugging flaky tests, or deciding what/how to test. |
Core philosophy: Test behavior, not implementation. Good tests survive refactoring, serve as documentation, and catch real bugs without false alarms.
"Write tests. Not too many. Mostly integration." — Guillermo Rauch
| Principle | Meaning |
|---|---|
| Fast | Milliseconds. Slow tests don't get run. |
| Isolated | Pass alone, in sequence, or parallel. |
| Repeatable | Same inputs = same results. No env/time deps. |
| Self-validating | Auto pass/fail. No human inspection. |
| Timely | Written close to (or before) code. |
test('calculates order total with tax', () => {
// Arrange
const order = new Order();
order.addItem({ price: 100 });
// Act
const total = order.calculateTotal({ taxRate: 0.1 });
// Assert
expect(total).toBe(110);
});
One behavior per test. Never Assert → Act → Assert in same test.
Good tests are:
Bad tests:
// BAD: tests implementation
expect(wrapper.state('openIndex')).toBe(1);
// GOOD: tests behavior
await userEvent.click(screen.getByText('Section 2'));
expect(screen.getByText('Section 2 content')).toBeVisible();
/\ E2E (5%)
/ \
/----\ Integration (15%)
/------\
/--------\ Unit (80%)
Favor when: Algorithmic code, stable interfaces, libraries, constrained resources.
E2E (critical paths)
/ \
/------\ Integration (largest)
/--------\
/----------\ Unit (smaller)
/______________\ Static (TS, ESLint)
Favor when: Apps with complex integrations, UI components, modern frontend.
Core insight: Integration tests offer best confidence-to-cost ratio for most apps.
| Layer | Test | Examples |
|---|---|---|
| Unit | Pure functions, algorithms, utils | formatCurrency(1234.5) → '$1,234.50' |
| Integration | Components together, API endpoints, DB ops | Cart calculates totals correctly |
| E2E | Critical user journeys only | Signup → purchase → confirmation |
| Contract | API compatibility between services | Consumer expectations match provider |
| Double | Purpose | Verification |
|---|---|---|
| Dummy | Fill params, never used | None |
| Fake | Working shortcut (in-memory DB) | State |
| Stub | Canned responses | State |
| Spy | Record calls + stub | State or Behavior |
| Mock | Verify specific calls made | Behavior |
Key distinction: Stubs verify state (what's the result?), mocks verify behavior (what calls were made?).
// BAD: mock everything
jest.mock('./userService');
jest.mock('./emailService');
jest.mock('./logger');
// Now testing that mocks return what you told them to
// GOOD: real implementations where practical
const db = createTestDatabase();
const service = new UserService(db); // real service, test DB
// Always passes, tests nothing
test('renders component', () => {
const wrapper = render(<MyComponent />);
expect(wrapper).toBeTruthy(); // Always true!
});
Fix: Watch test fail first. If you can't break it, it's worthless.
Fix: Test through public interfaces only.
Root causes:
sleep() vs explicit waits)Fixes:
// BAD
await sleep(3000);
// GOOD
await screen.findByText('Loaded');
await waitFor(() => expect(result).toBe(expected));
beforeEach, not afterEachHeavy manual testing → slow E2E → minimal unit tests.
Result: Late feedback, high cost, poor edge case coverage.
Names should read like specifications:
// GOOD
'calculates total price including tax'
'prevents adding out-of-stock items to cart'
'returns empty array when input is null'
// BAD
'test1'
'testCalculateTotal_ReturnsCorrectValue'
Tests don't have tests — they must be obviously correct on inspection.
// DRY helper for mechanics
const createTestUser = (overrides = {}) => ({
id: 1, name: 'Test User', ...overrides
});
// DAMP test with meaningful values
test('loyal customers receive 20% discount', () => {
const customer = createTestUser({ loyaltyYears: 5 }); // loyalty matters!
expect(calculateDiscount(customer)).toBe(0.20);
});
Coverage reveals untested code — useful for finding gaps.
Don't chase 100%: Produces trivial tests, tests without assertions, wasted time.
Target 70-85% as sanity check, not strict gate.
| Situation | Approach |
|---|---|
| Pure function with edge cases | Unit test |
| Component interactions | Integration test |
| Critical user journey | E2E test |
| External API | Mock at boundary |
| Internal collaborator | Real implementation |
| Flaky timing | Explicit waits, not sleep |
| Test breaks on refactor | Test behavior, not implementation |
| High coverage, bugs slip through | Check assertions, try mutation testing |
sleep() / arbitrary delays