Skip to content
intermediatePhase 40 · Testing

Test Strategy

Design a testing pyramid with unit, integration, and E2E tests.

45m
0 problems
Topic Progress0%

Testing Pyramid

Testing Pyramid

Balance between different test types.

The Pyramid

        /
       / E2E (10%) \
      /  Slow, Expensive  \
     /--------------------\
    / Integration (20%)     \
   /   Moderate Speed/Cost    \
  /--------------------------\
 / Unit Tests (70%)            \
/  Fast, Cheap, Many            \
/--------------------------------\

Test Distribution

Type % Speed Cost Confidence
Unit 70% Fast Low Low
Integration 20% Medium Medium Medium
E2E 10% Slow High High

Anti-Patterns

  1. Ice Cream Cone: More E2E than unit tests
  2. Funnel: All tests are E2E
  3. Hourglass: No integration tests

When to Use Each

Unit Tests:

  • Pure functions
  • Utility functions
  • Complex logic
  • Business rules

Integration Tests:

  • Component interactions
  • API integrations
  • State management
  • Form submissions

E2E Tests:

  • Critical user journeys
  • Checkout flows
  • Authentication
  • Cross-browser testing

What to Test First

What to Test First

Prioritize tests based on value.

Priority Matrix

  1. Critical business logic (highest priority)

    • Payment processing
    • Authentication
    • Data validation
  2. Complex components

    • Forms with validation
    • Interactive widgets
    • Data displays
  3. Edge cases

    • Error handling
    • Empty states
    • Loading states
  4. UI details (lowest priority)

    • Styling
    • Animations
    • Responsive design

Testing Checklist

## Must Test
- [ ] Authentication flows
- [ ] Payment processing
- [ ] Form validation
- [ ] Error handling
- [ ] Data CRUD operations

## Should Test
- [ ] Complex business logic
- [ ] Component interactions
- [ ] API integrations
- [ ] State management

## Nice to Test
- [ ] UI edge cases
- [ ] Responsive behavior
- [ ] Accessibility

TDD vs BDD

TDD vs BDD

TDD (Test-Driven Development)

1. Red: Write failing test
2. Green: Write minimal code to pass
3. Refactor: Improve code quality
// TDD Example
// 1. Write test first
it('should calculate total price', () => {
  expect(calculateTotal([10, 20, 30])).toBe(60);
});

// 2. Write implementation
function calculateTotal(prices) {
  return prices.reduce((sum, price) => sum + price, 0);
}

BDD (Behavior-Driven Development)

Given - Context
When - Action
Then - Expected outcome
// BDD Example
describe('Shopping Cart', () => {
  it('should calculate total when items are added', () => {
    // Given
    const cart = new ShoppingCart();
    
    // When
    cart.addItem({ price: 10 });
    cart.addItem({ price: 20 });
    
    // Then
    expect(cart.total).toBe(30);
  });
});

Comparison

Aspect TDD BDD
Focus Code behavior Business behavior
Language Technical Domain-specific
Collaboration Developers Everyone
Documentation Code Living docs

When to Use

Use TDD when:

  • Building complex algorithms
  • Working on infrastructure
  • Need precise code coverage

Use BDD when:

  • Cross-functional teams
  • Complex business rules
  • Need shared understanding

Tools

  • TDD: Jest, Vitest, Mocha
  • BDD: Cucumber, Jest with describe/it

Practice Problems

0/3solved
Build Test Strategy Component

Create a reusable React component implementing Test Strategy. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Test Strategy Testing

Write unit and integration tests for Test Strategy using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Test Strategy Performance

Optimize Test Strategy for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What is the ideal test distribution?

Question 1 options

2. What should you test first?

Question 2 options

3. What is the primary purpose of Test Strategy?

Question 3 options

4. What is a common mistake when implementing Test Strategy?

Question 4 options

Flashcards

Question

What is the testing pyramid?

Answer

A model suggesting 70% unit, 20% integration, 10% E2E tests for optimal balance.

Question

What is the ice cream cone anti-pattern?

Answer

Having more E2E tests than unit tests, making tests slow and expensive.

Question

What is the Red-Green-Refactor cycle?

Answer

TDD cycle: write failing test (Red), make it pass (Green), improve code (Refactor).

Question

What is the Given/When/Then format?

Answer

BDD format: Given context, When action, Then expected outcome.

Question

What is Test Strategy?

Answer

Test Strategy is a key concept in frontend development.

Revision Notes

Key Takeaways

  • 1.Follow the testing pyramid: 70% unit, 20% integration, 10% E2E
  • 2.Test critical business logic first
  • 3.TDD focuses on code behavior; BDD on business behavior
  • 4.Avoid anti-patterns like ice cream cone
  • 5.Balance speed, cost, and confidence

Interview Tips

  • Explain the testing pyramid and its distribution
  • Discuss when to use TDD vs BDD
  • Know how to prioritize what to test

Cheat Sheet

Test Strategy Cheat Sheet

Testing Pyramid

  • 70% Unit (fast, cheap)
  • 20% Integration (moderate)
  • 10% E2E (slow, expensive)

Priority

  1. Critical business logic
  2. Complex components
  3. Edge cases
  4. UI details

TDD

  • Red: Write failing test
  • Green: Make it pass
  • Refactor: Improve code

BDD

  • Given: Context
  • When: Action
  • Then: Outcome