Hone logo
Hone
Problems

Jest Test Suite for a Simple Calculator

This challenge focuses on building a robust test suite for a basic calculator function using Jest in TypeScript. Writing comprehensive tests is crucial for ensuring code reliability and preventing regressions as your codebase grows. You will practice identifying test cases, writing assertions, and structuring your tests effectively.

Problem Description

You are tasked with creating a TypeScript function that simulates a simple calculator. This function should accept two numbers and an operator as input and return the result of the operation. Your primary goal is to write a comprehensive test suite for this calculator function using the Jest testing framework.

Requirements:

  1. calculate Function: Create a TypeScript function named calculate that takes the following arguments:

    • num1: A number.
    • num2: A number.
    • operator: A string representing the operation ('+', '-', '*', '/').
    • The function should return the result of num1 operator num2.
  2. Jest Test Suite: Implement a Jest test suite for the calculate function. Your test suite should cover:

    • Basic Operations: Test addition, subtraction, multiplication, and division with positive and negative numbers.
    • Floating-Point Numbers: Ensure correct handling of operations involving floating-point numbers.
    • Division by Zero: Handle the case where num2 is zero during a division operation. The function should throw an error.
    • Invalid Operator: Handle cases where an invalid operator is provided. The function should throw an error.

Expected Behavior:

  • The calculate function should return the correct numerical result for valid inputs.
  • When division by zero is attempted, the calculate function should throw an Error with a specific message (e.g., "Division by zero is not allowed.").
  • When an invalid operator is provided, the calculate function should throw an Error with a specific message (e.g., "Invalid operator.").

Examples

Example 1: Addition

Input: calculate(5, 3, '+')
Output: 8
Explanation: 5 + 3 = 8

Example 2: Subtraction with Negative Numbers

Input: calculate(10, -4, '-')
Output: 14
Explanation: 10 - (-4) = 10 + 4 = 14

Example 3: Division

Input: calculate(15, 2, '/')
Output: 7.5
Explanation: 15 / 2 = 7.5

Example 4: Division by Zero

Input: calculate(10, 0, '/')
Output: Throws Error("Division by zero is not allowed.")
Explanation: Attempting to divide by zero should result in an error.

Example 5: Invalid Operator

Input: calculate(7, 2, '^')
Output: Throws Error("Invalid operator.")
Explanation: An unsupported operator should result in an error.

Constraints

  • The input numbers (num1, num2) will be valid numbers (integers or floating-point).
  • The operator will be a string.
  • Your test suite should aim for high test coverage, covering all specified scenarios.
  • Performance is not a critical concern for this challenge, but tests should be reasonably fast.

Notes

  • You will need to set up a Jest environment in a TypeScript project. This typically involves installing jest, ts-jest, and @types/jest as development dependencies.
  • Consider using describe and it (or test) blocks to organize your tests logically.
  • Use Jest's assertion methods (e.g., expect().toBe(), expect().toBeCloseTo(), expect().toThrow()) to verify the behavior of your calculate function.
  • For floating-point comparisons, toBeCloseTo() is generally preferred over toBe() due to potential precision issues.
  • When testing for thrown errors, use expect(() => ...).toThrow(). You can optionally specify the error message or type.
  • Think about how to make your calculate function reusable and export it so it can be imported into your test file.
Loading editor...
typescript