ThisCalculator

Everyday

Test Relay Delivery Tasks in Node.js Without a Real Rider

Updated · Standard formula, shown below

Elapsed time 30.00 min
Progress fraction
100 %
Result
Total test duration
30 min
You entered
Simulated rider speed
20 km/h
You entered

Test Relay Delivery Tasks in Node.js Without a Real Rider

Yes, you can fully test relay delivery tasks in Node.js without a real rider by mocking the rider’s location, speed, and route with a simulated GPS feed. This article explains how to build a test harness that emulates a courier moving along a route, so you can verify task assignment, progress updates, and completion logic in a deterministic way.

Formula

The core simulation uses the basic motion equation: time = distance / speed. In the calculator above, the simulated rider travels a fixed route distance d (in km) at a constant speed s (in km/h). The elapsed time t (in minutes) is the test duration you want to simulate. The progress fraction is calculated as:

progress (%) = min(100, (t / (d / (s / 60))) * 100)

Where d / (s / 60) gives the total time needed to complete the route in minutes. This lets you compute how far along the rider should be at any moment during the test.

Worked Example

Using the default values: route distance = 10 km, rider speed = 20 km/h, test duration = 30 minutes.

  1. Convert speed to km/min: 20 km/h ÷ 60 = 0.3333 km/min
  2. Total time to complete route: 10 km ÷ 0.3333 km/min = 30 minutes
  3. At t = 30 minutes, progress = min(100, (30 / 30) * 100) = 100%

The calculator outputs 30 minutes for elapsed time and 100% progress, confirming the rider has completed the route.

Why Mock a Rider?

Real rider testing requires physical devices, GPS hardware, network connectivity, and a person actually moving. That’s slow, expensive, and non-deterministic. By mocking the rider, you can run automated tests in CI/CD pipelines, simulate edge cases (like rider stops or detours), and verify your relay logic without any external dependency.

Common scenarios where mocking is essential:

  • Testing task reassignment when a rider drops out
  • Verifying that progress updates are sent to the client at correct intervals
  • Checking that the system handles GPS signal loss gracefully
  • Stress-testing with many concurrent simulated riders

How to Simulate a Rider’s Location

The standard approach is to create a mock GPS service that emits location updates at a fixed interval. In Node.js, you can use a simple setInterval that increments the rider’s position along a predefined polyline (list of lat/lng points). For each tick, you calculate the new position based on elapsed time and speed.

Here’s a minimal example:

const route = [[lat1, lng1], [lat2, lng2], ...];
let currentIndex = 0;
let distanceTraveled = 0;

setInterval(() => {
  // move along route based on speed
  // emit { lat, lng, timestamp } to your task handler
}, 1000); // every second

For more accurate simulation, use the turf.js library to interpolate points along a line, or the geolib library to compute distances and bearings.

Building a Test Harness for Relay Tasks

A relay delivery task typically involves multiple legs: rider A picks up a package, rides to a handoff point, then rider B takes over. To test this without real riders, you need to simulate each rider independently and coordinate their handoffs.

  1. Define the route segments – each rider has a start, handoff, and end point.
  2. Create a mock rider class – with properties like id, speed, current position, and status (idle, moving, waiting).
  3. Emit location updates – at a fixed frequency (e.g., every second) and feed them into your task assignment logic.
  4. Trigger handoffs – when a rider reaches the handoff point, mark the leg complete and activate the next rider.
  5. Assert outcomes – check that the task status changes correctly and that all events fire in order.

You can use a test framework like Jest or Mocha to run these simulations and assert the final state. For example, you might verify that the task reaches “completed� only after all legs are done.

Common Pitfalls and How to Avoid Them

Even with mocking, several issues can break your tests:

  • Time drift – if your simulation uses real timers, the test can be flaky. Use fake timers (e.g., sinon.useFakeTimers()) to control time deterministically.
  • Rounding errors – GPS coordinates and distances need consistent precision. Use a fixed number of decimal places (e.g., 6) to avoid mismatches.
  • Speed variation – real riders don’t move at constant speed. Simulate acceleration/deceleration by adjusting the interval or distance per tick.
  • Handoff race conditions – ensure that location updates are processed sequentially, or use an async queue to avoid overlapping events.

A common misconception is that you need a full map engine to simulate movement. In reality, a simple linear interpolation between waypoints is sufficient for most logic tests.

Tools and Libraries to Use

Several open-source libraries make this easier:

  • turf.js – for geometric operations like along() to get a point at a given distance along a line.
  • geolib – for distance and bearing calculations between coordinates.
  • sinon – for fake timers and spies to verify event calls.
  • jest – for test running and assertions.

You can also use a simple in-memory event emitter to simulate the GPS feed, avoiding any external dependencies.

Alternatives to Full Simulation

If you don’t need to test the entire relay flow, consider lighter alternatives:

  • Unit tests – mock the GPS module entirely and test only the task state machine.
  • Contract tests – verify that your system sends the correct API calls when a rider updates location.
  • Manual integration with a GPS simulator app – some mobile apps can broadcast fake locations, but this still requires a device.

For most teams, a pure Node.js simulation is the fastest and most reliable option.

Frequently Asked Questions

Can I simulate multiple riders at once?

Yes, create an array of mock rider instances, each with its own timer and route. Ensure that your event loop can handle the frequency of updates; you may need to increase the interval or use a single loop that updates all riders sequentially.

How do I test handoff logic without a real rider?

Simulate the first rider reaching the handoff point by setting its position exactly to that coordinate. Then trigger the handoff event and verify that the second rider starts moving from that point. You can also test edge cases like early or late handoffs.

What if my route has many waypoints?

Use a library like turf.js to precompute the cumulative distances along the route. Then, for a given elapsed time, calculate the distance traveled and find the corresponding segment and interpolated point.

Is it safe to use real timers in tests?

No, real timers make tests slow and flaky. Use fake timers to control time precisely. With Jest, you can call jest.useFakeTimers() and then advance time manually with jest.advanceTimersByTime().

How do I verify that progress updates are sent to the client?

Spy on your WebSocket or HTTP callback function. After each simulated location update, assert that the callback was called with the expected payload (lat, lng, progress). This ensures your server is emitting the right events.

Can I simulate GPS loss or signal degradation?

Yes, in your mock rider, you can randomly omit location updates or add noise to the coordinates. This helps test how your system handles missing or inaccurate data.

Related

Formula used
in t | Total test duration | min | 30 | 1
in s | Simulated rider speed | km/h | 20 | 1
in d | Route distance | km | 10 | 0.1
out elapsed | Elapsed time | min | d / (s / 60) | 2
out progress | Progress fraction | % | min(100, (t / (d / (s / 60))) * 100) | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.