StoryNovember 3, 20252 min read

Writing Your First Jest Test (Step-by-Step Guide)

Let’s start fresh with a tiny project folder.


Writing Your First Jest Test (Step-by-Step Guide)

In the previous post, we understood what testing really means and why we use Jest.
Now it’s time to get practical — let’s write our first Jest test together.
We’ll continue with the same Expense Tracker example we started earlier so that every concept stays connected and easy to remember.
Designed by Author in Figma

Setting Up Jest for Our Expense Tracker

Let’s start fresh with a tiny project folder.

mkdir expense-tracker-tests
cd expense-tracker-tests
npm init -y

Now install Jest as a development dependency:

npm install --save-dev jest

Next, open your package.json and add this script:

"scripts": {
  "test": "jest"
}

This tells Node that whenever we type npm test, it should run Jest.

✅ Now you can test your setup by running:

yarn test

If everything is fine, Jest will show:

No tests found, exiting with code 1

That’s okay — we haven’t written any tests yet.

Creating Our Expense Function

Inside the same folder, create a new file named getTotal.js:

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

This function adds up all our expenses.
We’ll use it across the whole series — like the heart of our Expense Tracker.

Writing Our First Test File

Now, create another file named getTotal.test.js.

Add this code inside:

const { getTotal } = require('./getTotal');

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

Let’s understand what’s happening here:

  • test() defines one small check for your code.
  • Inside it, we create some mock expenses — [100, 200].
  • Then we call our getTotal() function.
  • Finally, expect(result).toBe(300) checks if the total is correct.

That’s your first test.

Simple, clean, and directly connected to your Expense Tracker.

Running the Test

Now, run the command:

npm test

If everything works, you’ll see something like this:

PASS  ./getTotal.test.js
 ✓ should return total of all expenses (3 ms)

🎉 Congratulations — you just wrote and passed your first Jest test!

Understanding What Happened

You didn’t have to log anything.
No manual checking.
Jest did all the validation for you in milliseconds.

Think of Jest as your Expense Tracker’s safety guard — 
it double-checks that your totals, calculations, and logic never go wrong,
even months after you’ve written the code.

Why This Matters

Without tests, every code change becomes risky.
With Jest, your Expense Tracker’s logic stays reliable forever.
If tomorrow you modify how expenses are added, Jest will instantly tell you if something breaks.

Testing = Peace of mind. 🧠

✅ Summary

In this post, we:

  • Installed and set up Jest
  • Created our getTotal() function
  • Wrote our first test
  • Verified that it runs successfully

Next, we’ll go one step further —

We’ll test multiple functions and handle different expense cases (like empty lists, negative values, and data errors).

This is where Jest starts to feel powerful.


💬 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