Unit Test Suite for a Math Utility
What you'll build
A math utility module with functions for addition, subtraction, multiplication, division, and factorial, along with a comprehensive Jest test suite covering normal cases, edge cases, and error handling.
Goals
- Write a math utility module with 5 functions
- Create a Jest test suite with at least 15 test cases
- Cover normal inputs, edge cases (zero, negative), and error cases (division by zero)
- Use the Arrange-Act-Assert pattern consistently
- Achieve 100% statement coverage
Prerequisites
- Basic JavaScript (functions, arithmetic, error handling)
- Node.js and npm installed
- Understanding of what a unit test is
Steps
- 1
Set up the project
Create a new directory, run
npm init -y, and install Jest:npm install --save-dev jest. Add a test script to package.json:"test": "jest".Code{ "scripts": { "test": "jest" } }Expected result
package.json has a test script and jest is in devDependencies.
- 2
Create the math utility module
Create
math.jswith functions: add, subtract, multiply, divide (throws on division by zero), and factorial (throws on negative input).Codefunction add(a, b) { return a + b; } function subtract(a, b) { return a - b; } function multiply(a, b) { return a * b; } function divide(a, b) { if (b === 0) throw new Error('Division by zero'); return a / b; } function factorial(n) { if (n < 0) throw new Error('Negative input'); if (n <= 1) return 1; return n * factorial(n - 1); } module.exports = { add, subtract, multiply, divide, factorial };Expected result
math.js exports all five functions.
- 3
Write tests for add and subtract
Create
math.test.js. Write tests for add (positive, negative, zero) and subtract using the AAA pattern.Codeconst { add, subtract } = require('./math'); describe('add', () => { test('adds two positive numbers', () => { expect(add(2, 3)).toBe(5); }); test('handles negative numbers', () => { expect(add(-1, -1)).toBe(-2); }); test('handles zero', () => { expect(add(0, 5)).toBe(5); }); });Expected result
Run
npm test— all add and subtract tests pass. - 4
Write tests for divide and factorial
Add tests for divide (including the error case) and factorial (including negative error case). Use toThrow for error cases.
Codedescribe('divide', () => { test('divides correctly', () => { expect(divide(10, 2)).toBe(5); }); test('throws on division by zero', () => { expect(() => divide(1, 0)).toThrow('Division by zero'); }); });Expected result
All tests pass, including error cases.
- 5
Check coverage
Run
npx jest --coverageand verify 100% statement coverage.Expected result
Coverage report shows 100% statements, branches, functions, and lines.
Hint
If coverage is below 100%, add tests for uncovered branches.
Try it yourself
What you learned
You learned how to structure a unit test suite using Jest, apply the Arrange-Act-Assert pattern, test error cases with toThrow, and measure code coverage. These skills form the foundation of test-driven development.
Related Resources
Related Lessons
- Unit Testing
Writing effective unit tests.
- Jest Basics
Getting started with Jest.
Related Practice
- Testing MCQ Exercises
Practice testing concepts.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.