Testing in Jest: Building and Testing A Calculator in JavaScript

Steven Kandow
3 min readDec 2, 2020

--

Return of the calculator

A couple weeks ago, I took a look at the process for building a basic calculator in Ruby and testing it using RSpec. This week, we’ll take our calculator and move it to JavaScript, testing it with one of the most popular JavaScript testing libraries: Jest.

In this instance, rather than building a class, we’ll simply keep our functions in the same file, called calculator.js.

If you want to follow along, set up your directory using the following steps:

  1. Create the directory. Name it whatever you’d like. (Mine is named CalculatorTestJest.) Enter the directory in your terminal.
  2. Run npm init
  3. Run npm install --save-dev jest
  4. Run touch calculator.js
  5. Run touch calculator.test.js
  6. Open your director in your preferred editor. In your package.json file, be sure that the following is included in the file:
"scripts": {
"test": "jest"
}

Okay! We are ready to create our first test!

First, let’s be sure that no matter what we write in our calculator.js file, we’ll be able to utilize it in this test file.

in calculator.test.js
---------------------
const calculator = require.('./calculator);

We’ll eventually create an exports object in our calculator.js file that will contain all the methods we’re going to test. With that in mind, let’s write the first test!

in calculator.test.js
---------------------
const calculator = require.('./calculator);
const x = 13;
const y = 5;
test(‘Add numbers using the add method’, () => {
expect(calculator.add(x, y)).toBe(18)
});

Let’s take a look at the format above:

  1. Use the keyword test, which takes two arguments: the first is a string that should outline clearly what is being tested; the second is the function that does the testing.
  2. For this particular function, the keyword expect connects what we are testing with the keyword toBe, which is the keyword that reveals what we expect the answer to be.

Be sure to check out the “matchers” library via the Jest documentation to see all the possibilities for how expect can be utilized.

To run this test in your terminal, type npm test.

Like RSpec with Ruby, this test is quite helpful in telling us what has occurred here. We need an add function in our calculator.js file, so let’s write it.

in calculator.js
---------------------
function add(a, b) {
return a + b;
}
module.exports.add = add;

Notice what we’re doing here. We’ve written the function, and we’ve set a key-value pair in our exports object of the function add().

Let’s run our tests now.

And we’re seeing green!

Try to write out tests for the following functions: subtract(), multiply(), divide(), and modulo(). Write the tests first, then write the functions.

Be sure to try these on your own before taking a look below.

in calculator.test.js
---------------------
const calculator = require('./calculator');const x = 13;
const y = 5;
test('Add numbers using the add method', () => {
expect(calculator.add(x, y)).toBe(18)
});
test('Subtract numbers using the subtract method', () => {
expect(calculator.subtract(x, y)).toBe(8)
});
test('Multiply numbers using the multiply method', () => {
expect(calculator.multiply(x, y)).toBe(65)
});
test('Divide numbers using the divide method', () => {
expect(calculator.divide(x, y)).toBe(2.6)
});
test('Find the remainder of dividing numbers using the modulo method', () => {
expect(calculator.modulo(x, y)).toBe(3)
});
in calculator.js
---------------------
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
return a / b;
}
function modulo(a, b) {
return a % b;
}
module.exports.add = add;
module.exports.subtract = subtract;
module.exports.multiply = multiply;
module.exports.divide = divide;
module.exports.modulo = modulo;

And when we run the tests:

Happy coding and testing!

--

--