Continue Developing the calculateDiscount Function

Introduction to TDD and Jest

Welcome to your second lesson for this course dedicated to practicing Test Driven Development (TDD) utilizing TypeScript and Jest. In this unit, we will continue adding featurs to our calculateDiscount function. We have 5 more requirements for you to implement!

In this course, emphasis is placed on hands-on practice, where you'll receive requirements through tests, one at a time. Your task is to implement code that makes each test pass, simulating a real-world TDD environment. As the course guide, it's akin to being your pair programmer, providing test-driven prompts to hone your skills as you progress.

Understanding the Red-Green-Refactor Cycle

As a reminder, the Red-Green-Refactor cycle is central to TDD, guiding your development process:

  • Red: Start by writing a failing test to define the next step.
  • Green: Implement just enough code to pass the test, focusing on functionality.
  • Refactor: Optimize the code for clarity and efficiency without changing its behavior.

More Requirements for `calculateDiscount` Function

7. Non-Numeric Price Input Handling

  • Description: The function should validate that the price is a numeric value and throw an error if a non-numeric input is provided.
  • Test Case:
    TypeScript
    it('should throw an error for non-numeric price inputs', () => {
      // Arrange
      const originalPrice = 'invalid' as any;
      const discountPercentage = 10;
    
      // Act, Assert
      expect(() => calculateDiscount(originalPrice, discountPercentage)).toThrow('Price must be a valid number');
    });

8. Non-Numeric Discount Input Handling

  • Description: The function should validate that the discount percentage is a numeric value and throw an error if a non-numeric input is provided.
  • Test Case:
    it('should throw an error for non-numeric discount inputs', () => {
      // Arrange
      const originalPrice = 100;
      const discountPercentage = 'invalid' as any;
      
      // Act, Assert
      expect(() => calculateDiscount(originalPrice, discountPercentage)).toThrow('Discount must be a valid number');
    });
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal