Unit Testing Project For A Simple Calculator

This project employs Jest for unit testing and code coverage analysis, by testing many cases in order to perform the needed requirements.

Steps to clone the repository and generate code coverage using npm::

  1. Clone the GitHub Repository: git clone https://github.com/Rahaf-Mansour/unitTesting-calculator.git
  2. Install Dependencies: cd unitTesting-calculator then npm install
  3. Run Unit Tests: npm test
  4. Generate Code Coverage: npm run coverage

Project Requierments:

  1. Create a simple calculator function.
  2. You need to pass these test cases, it is important to properly implement the calc function.
// calculator.test.js
const calc = require('./calculator');

describe('Calculator', () => {
  // Test case: Addition
  it('should return the correct sum of two numbers', () => {
    expect(calc(2, '+', 3)).toBe(5);
  });

  // Test case: Subtraction
  it('should return the correct difference of two numbers', () => {
    expect(calc(5, '-', 2)).toBe(3);
  });

  // Test case: Multiplication
  it('should return the correct product of two numbers', () => {
    expect(calc(4, '*', 6)).toBe(24);
  });

  // Test case: Division
  it('should return the correct quotient of two numbers', () => {
    expect(calc(10, '/', 2)).toBe(5);
  });

  // Test case: Division by zero
  it('should throw an error when dividing by zero', () => {
    expect(() => calc(6, '/', 0)).toThrow('Division by zero');
  });

  // Test case: Negative numbers
  it('should handle negative numbers correctly', () => {
    expect(calc(-8, '+', 5)).toBe(-3);
  });

  // Test case: Decimal numbers
  it('should handle decimal numbers correctly', () => {
    expect(calc(3.5, '*', 2)).toBe(7);
  });

  // Test case: Order of operations
  it('should follow the correct order of operations', () => {
    expect(calc(2, '+', 3, '*', 4)).toBe(14);
  });

  // Test case: Invalid operator
  it('should throw an error for an invalid operator', () => {
    expect(() => calc(5, '$', 3)).toThrow('Invalid operator');
  });

  // Test case: Invalid input type
  it('should throw an error for invalid input types', () => {
    expect(() => calc('2', '+', 3)).toThrow('Invalid input type');
  });
});
  1. To enhance the test coverage and ensure thorough testing, add additional test cases. It is advised to intentionally make these new test cases fail initially. This approach allows for more robust validation of the calc function implementation. Once the new test cases fail, proceed with implementing the necessary changes in the calc function to make them pass.

  2. Allow calc to handle an unknown amount of numbers.

  3. Numbers bigger than 1000 should be ignored, so adding 2 + 1001 = 2

Take A Look:

image

Acknowledgements

I extend my sincere gratitude to Foothill Technology Solutions for granting me the opportunity to participate in this internship cycle. Their unwavering support has been instrumental throughout the development of this project.

fts-logo