StoryDecember 31, 20253 min read

šŸ—ŗļø 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

Image generated by Author usingĀ ChatGPT

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

Image generated by Author usingĀ ChatGPT

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

Image generated by Author usingĀ ChatGPT

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

Image generated by Author usingĀ ChatGPT

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