StoryNovember 4, 20253 min read

Testing Multiple Functions in Jest — Step by Step

Let’s see what happens when your Expense Tracker starts growing.


Testing Multiple Functions in Jest — Step by Step

Designed by Author in Figma

You thought writing one test was simple?

Let’s see what happens when your Expense Tracker starts growing.

In the previous post, we tested one small function — getTotal().
But real-world apps are rarely that simple.
They calculate, compare, filter, and sometimes even break.
Today, we’ll take our Expense Tracker one step ahead and learn how to test multiple functions using Jest.

🧾 Our Expense Tracker Grows

Every app evolves — and so does ours.

Let’s say we now want to track:

  • 💰 The total expenses
  • 🏆 The highest expense
  • ⚖️ The average expense

We’ll create new helper functions for each of these inside a single file.

🛠️ Step 1 — Create Multiple Functions

Create a file named expenseUtils.js and add this code:

exports.getTotal = (list) => {
  return list.reduce((a, b) => a + b, 0);
};

exports.getHighestExpense = (list) => {
  return Math.max(...list);
};

exports.getAverageExpense = (list) => {
  if (list.length === 0) return 0;
  return exports.getTotal(list) / list.length;
};

Each function now handles one small job

And together, they make our Expense Tracker smarter.

🧪 Step 2 — Write Tests for All Functions

Now create a file named expenseUtils.test.js and add this:

const {
  getTotal,
  getHighestExpense,
  getAverageExpense,
} = require('./expenseUtils');

describe('Expense Tracker Functions', () => {
  test('should return total of all expenses', () => {
    const result = getTotal([100, 200, 300]);
    expect(result).toBe(600);
  });

  test('should return highest expense', () => {
    const result = getHighestExpense([100, 250, 400]);
    expect(result).toBe(400);
  });

  test('should return average expense', () => {
    const result = getAverageExpense([100, 200, 300]);
    expect(result).toBe(200);
  });
});

Here’s what’s new:

  • describe() groups related tests.
  • Each test() checks one function.
  • Jest automatically runs all of them and reports results in one place.

⚡ Step 3 — Run All Tests Together

Run this in your terminal:

npm test

You should see this output:

PASS  ./expenseUtils.test.js
  Expense Tracker Functions
    ✓ should return total of all expenses (3 ms)
    ✓ should return highest expense (2 ms)
    ✓ should return average expense (1 ms)

✅ Boom! Jest just tested three functions in one go.

That’s the power of automation — one command, multiple checks, zero stress.

💡 Step 4 — Why Use describe()

The describe() block is not mandatory, but it’s incredibly useful.

It groups related tests together — like folders for your test cases.

So instead of seeing a long list of mixed test names,

you get neatly organized results grouped under “Expense Tracker Functions.”

Later, when your app has dozens of files,

you’ll thank yourself for keeping things this clean.

🧠 Step 5 — Why Grouping Tests Matters

In real-world projects, you’ll often work with:

  • Separate modules (user, expense, auth, reports, etc.)
  • Multiple developers testing different logic
  • Continuous Integration tools running tests automatically

Grouping makes everything clear, organized, and scalable.

One day, when your Expense Tracker becomes a full-blown finance app,
your test structure will already be rock solid — thanks to describe().

✅ Summary

Today, you learned:

  • How to test multiple functions in one file
  • How to use describe() for cleaner test organization
  • Why grouping tests improves scalability

In the next post, we’ll take on error handling — because real data isn’t always perfect.

We’ll learn how Jest catches mistakes and keeps your Expense Tracker safe from bad inputs.


💬 Comment what confused you most about testing — I’ll cover it in the next post.

Thanks for reading.

I explain things in simple words so learning stays easy today and even years later.
If this added value, follow me for more clear and practical posts.
— Alkesh Jethava