š§© Mocking Modules and Dependencies in Jest ā Expense Tracker Edition
Our Expense Tracker is growing like a startup that just got funding.
š§© Mocking Modules and Dependencies in JestāāāExpense TrackerĀ Edition

Our Expense Tracker is growing like a startup that just got funding.
It has logic, tests, even spies watching its every move.
But now, it is hiring new āemployeesā helpers, modules, and third-party tools.
And like every team in the world, not all new hires behave. š
Some work perfectly on day one.
Some break things the moment youāre not watching.
And some refuse to come to work at all.
Thatās why we need mocking for modulesāāāa way to test your code without trusting your teammates too much.
š” The Problem with Dependencies

Letās be honest.
No developer ever said,
āWow, I love debugging this third-party library I did not write!ā
Your code might depend on something tinyāāālike a utility that converts currency,
but if that utility breaks, your tests explode like fireworks.
So instead of letting those modules run wild, we replace them with perfect clones that behave exactly how we want.
Thatās Jest mockingāāāor as I like to call it:
āProfessional-level lying for good reasons.ā š
āļø Step 1āāāMeet the NewĀ Employee
š currencyConverter.js
exports.convertToINR = (usd) => {
// Imagine this calls an external API
console.log('Converting USD to INR...');
return usd * 83;
};And hereās the main code that uses it:
š expenseCurrency.js
const { convertToINR } = require('./currencyConverter');
exports.calculateExpenseInINR = (usdAmount) => {
const inr = convertToINR(usdAmount);
return { usd: usdAmount, inr };
};This looks fine, until one day the API crashes or returns undefined.
Your tests fail. Your morning coffee tastes bitter. ā
We can fix that.
š§Ŗ Step 2āāāFire the Real One, Hire a MockĀ š
š expenseCurrency.test.js
jest.mock('./currencyConverter', () => ({
convertToINR: jest.fn(),
}));
const { calculateExpenseInINR } = require('./expenseCurrency');
const { convertToINR } = require('./currencyConverter');
test('should use mocked convertToINR', () => {
convertToINR.mockReturnValue(8300);
const result = calculateExpenseInINR(100);
expect(convertToINR).toHaveBeenCalledWith(100);
expect(result.inr).toBe(8300);
});ā
We just replaced the real convertToINR with a fake one that always behaves.
It never forgets, never breaks, never calls the internet.
Itās like the worldās best intern.
Imagine Explaining This to aĀ Non-Dev

āSo, you fired your real function and replaced it with a fake one that pretends to do the job?ā
āExactly!ā
āAnd this makes you trust your code more?ā
āYes.ā
āYouāre insane.ā
Welcome to testing. š
š§ Step 3āāāMocking Third-Party Packages
Letās take it up a notch.
Say your app uses axios for API calls.
You donāt want your test waiting for the internetāāāso you replace it too.
š expenseApi.test.js
jest.mock('axios');
const axios = require('axios');
const { fetchExpenses } = require('./expenseApi');
test('should mock axios get call', async () => {
axios.get.mockResolvedValue({ data: [{ id: 1, amount: 500 }] });
const result = await fetchExpenses();
expect(result.length).toBe(1);
});Boom š„
you just controlled axios itself.
You told it what to say, and it obeyed like a polite bot.
āļø Step 4āāāThe Jedi Trick: PartialĀ Mocking
Sometimes you donāt want to replace the whole module, just one noisy function inside it.
Thatās where partial mocking comes in.
jest.mock('./currencyConverter', () => {
const originalModule = jest.requireActual('./currencyConverter');
return {
...originalModule,
convertToINR: jest.fn().mockReturnValue(9000),
};
});Now, you keep everything else real, but replace one part.
Itās like hiring a clone whoās just a little smarter at one thing.
š§© Step 5āāāDonāt Forget to Clean theĀ Mess
After every test, always restore order:
afterEach(() => {
jest.resetAllMocks();
});Itās like telling your test suite,
āAlright team, breakās overāāāback to normal behavior!ā
This keeps your next tests clean and drama-free.
ā Summary

Today,
Our Expense Tracker learned how to manage its relationships like a pro.
It now knows which functions to trust, which to replace, and when to keep things real.
We mocked:
- Our own modules
- Third-party libraries like Axios
- And even learned partial mocking for finer control
In short,
our code stopped chasing bad dependenciesāāāit hired replacements that never fail. š
In the next Part we will see Mocking Configurations and Chained Dependencies in Jestāāāwhere your app will start mocking its own brain.
š¬ Comment which part made you laugh (or cry) the mostāāāI read them all.
Thanks forĀ reading.
If this added value, follow me for more clear and practical posts.
ā Alkesh Jethava