šŗļø Mocking GPS & Location Sensors in Jest ā Expense Tracker Edition
It helps with sorting, filtering, statistics ā even memory.
šŗļø Mocking GPS & Location Sensors in JestāāāExpense TrackerĀ Edition

Our Expense Tracker has:
- survived storage
- time travel
- mocked APIs
- mocked notifications
- become emotionally strong
Now it wants to be location-aware.
It wants to say:
āHey, you spent ā¹180 at Iscon Mall today.ā
Makes sense, right?
Modern apps auto-tag location.
It helps with sorting, filtering, statisticsāāāeven memory.
But there is a problem.
A universal problem.
GPS sensors have one personality: unpredictable.
š” Why GPS Sensors NeedĀ Mocking

Because real GPS behaves like this:
- Works fine in open area
- Dies instantly near a kitchen
- Drifts 300 meters for no reason
- Takes 14 seconds inside malls
- Shows you in another city sometimes
- Fails exactly when you want to demo to your manager
- Gives 0,0 (the ocean) randomly
- Takes forever on some cheap Android phones
Testing with real GPS?
Impossible.
Testing with Jest?
Easy.
Predictable.
Fast.
No actual satellites required.
š§Ŗ Step 1āāāOur Expense Trackerās LocationĀ Feature
Letās say our app does:
// location.js
exports.getLocation = () => {
return navigator.geolocation.getCurrentPosition(
(success) => success.coords,
(error) => null
);
};And our app uses this when adding an expense:
exports.addExpense = async (amount) => {
const coords = await getLocation();
return { amount, location: coords };
};Simple.
But deadly.
Because if GPS breaks ā your whole test breaks.
š§Ŗ Step 2āāāMocking GPSĀ Success
š location.test.js
const mockGeolocation = {
getCurrentPosition: jest.fn((success) =>
success({ coords: { latitude: 23.0225, longitude: 72.5714 } })
),
};
global.navigator = { geolocation: mockGeolocation };
const { getLocation } = require('./location');
test('should return mocked coordinates', () => {
const coords = getLocation();
expect(coords.latitude).toBe(23.0225);
});We just pretended Ahmedabadās coordinates came from a satellite.
But really ā Jest produced them in 0.0001 seconds.
Pain
You test GPS.
Ā It works.
Ā You restart the device.
Ā It stops.
Ā You go outside.
Ā It works.
Ā You come back inside.
Ā It moves your location 200 meters away.
Ā You try to debug.
Ā It tells you nothing.
Ā You Google.
Ā Everyone says āworks for me.ā
GPS is not a sensor.
Ā It is a ghost. š»
Thatās why mocking feels⦠comforting.
š§Ŗ Step 3āāāMocking GPSĀ Failure
When user denies permission
OR
device canāt find location:
mockGeolocation.getCurrentPosition.mockImplementationOnce((success, error) =>
error({ message: 'Location unavailable' })
);Now your app must handle:
- null locations
- missing coordinates
- fallback behavior
This makes your app real-world ready.
š§Ŗ Step 4āāāMocking Slow GPS Responses
Some devices take forever:
jest.useFakeTimers();
mockGeolocation.getCurrentPosition.mockImplementation((success) => {
setTimeout(() => success({ coords: { latitude: 0, longitude: 0 } }), 5000);
});
jest.runAllTimers();Five seconds ā one millisecond.
āTime is an illusion.ā
Psychology

GPS bugs are painful because they feel random:
- Some phones instantly give coordinates
- Others take eternity
- Cheap devices return inaccurate values
- Indoor locations break
- Malls confuse sensors
- Sometimes GPS claims youāre in Mumbai when youāre in Ahmedabad
This pain hooks emotionally.
Summary

Your Expense Tracker is now location-aware without touching real sensors.
Today we learned:
š§ Mocking GPS success
Ā ā Mocking GPS failure
Ā š¢ Mocking slow responses
Ā š Predictable coordinates in tests
Ā š°ļø Zero dependency on real hardware
Your tests no longer rely on:
- satellites
- devices
- permissions
- sunlight
- hope
They rely only on YOU.
Thanks forĀ reading.
If this added value, follow me for more clear and practical posts.
ā Alkesh Jethava