settingsAccountsettings
Menusettings

Q: Add / Subtract (unit testing task with Mocha and Chai)

+1 vote

Write Mocha tests to check the functionality of the following JS code:

function createCalculator() {
    let value = 0;
    return {
        add: function (num) {
            value += Number(num);
        },
        subtract: function (num) {
            value -= Number(num);
        },
        get: function () {
            return value;
        }
    }
}

//console.log(createCalculator().get());

module.exports = {createCalculator};

Your tests will be supplied a function named 'createCalculator'. It needs to meet the following requirements:

  • Returns a module (object), containing the functions add, subtract and get as properties
  • Keeps an internal sum which can’t be modified from the outside
  • The functions add and subtract take a parameter that can be parsed as a number (either a number or a string containing a number) that is added or subtracted from the internal sum
  • The function get returns the value of the internal sum
asked in JavaScript category by user andrew

1 Answer

0 votes

My Mocha/Chai tests:

let createCalculator = require("../07.AddSubtract").createCalculator;
let expect = require("chai").expect;

describe("Tests for this task", function () {

    let calc;
    beforeEach(function () {
        calc = createCalculator();
    });

    describe("Function tests - checks if it is a function", function () {
        it("should be a function", function () {
            expect(typeof calc).to.equal("object");
        })
    });

    describe("Zero value when created", function () {
        it("should zero value when created", function () {
            expect(calc.get()).to.equal(0);
        })
    });

    describe("Check add function", function () {
        it("Check add function", function () {
            calc.add(3);
            calc.add(4);
            expect(calc.get()).to.equal(7);
        })
    });

    describe("Check subtract function", function () {
        it("Check subtract function", function () {
            calc.subtract(5);
            calc.subtract(4);
            expect(calc.get()).to.equal(-9);
        })
    });

    describe("Check add function with fractions", function () {
        it("Check add function with fractions", function () {
            calc.add(3.14);
            calc.add(4.14);
            expect(calc.get()).to.equal(7.279999999999999);
        })
    });

    describe("Check subtract function with fractions", function () {
        it("Check subtract function with fractions", function () {
            calc.subtract(-3.14);
            calc.subtract(-4.14);
            expect(calc.get()).to.equal(7.279999999999999);
        })
    });

    describe("Check incorrect input for add", function () {
        it("Check incorrect input for add", function () {
            calc.add("pesho");
            expect(calc.get()).to.be.NaN;
        })
    });

    describe("Check incorrect input for subtract", function () {
        it("Check incorrect input for subtract", function () {
            calc.subtract("pesho");
            expect(calc.get()).to.be.NaN;
        })
    });

    describe("Check empty input", function () {
        it("Check empty input", function () {
            calc.subtract();
            expect(calc.get()).to.be.NaN;
        })
    });

    describe("Check both functions (add/subtract) with strings", function () {
        it("Check both functions (add/subtract) with strings", function () {
            calc.add("pesho");
            calc.subtract("gosho");
            expect(calc.get()).to.be.NaN;
        })
    });

    describe("Should work with numbers as strings", function () {
        it("Should work with numbers as strings", function () {
            calc.add("7");
            expect(calc.get()).to.equal(7);
        })
    });
});

 

answered by user mitko
...