DEV Community

Cover image for How to Generate Realistic E-Commerce Test Data in 2 Lines of Code
Phạm Trần Gia Hưng
Phạm Trần Gia Hưng

Posted on

How to Generate Realistic E-Commerce Test Data in 2 Lines of Code

Generate Realistic E-Commerce Test Data

Some "fake data" libraries give a independent random objects. Call faker.commerce.product() a hundred times and you get a hundred products but nothing connects them. If you need a realistic dataset (carts that reference real users, orders that reference real carts, shipments that reference real orders, total that add up), you end up hand-wiring all of that yourself in a seed script.

My project eco-faker does the wiring for you. Its a stateful, relationally-consistent fake-data generator built specifically for e-commerce: every Cart, Order, Shipment, and ReturnRequest comes out of the same underlying state machine, so the dataset reads like a real store's history instead of unrelated fixtures.

The 2 lines

npm install -g eco-faker
my-eco-gen generate --users 100 --format sql --output ./seed.sql
Enter fullscreen mode Exit fullscreen mode

That a SQL seed file with 100 users and everything downstream of them: carts, abandoned checkouts, orders, shipments, returns, all internally consistent. If you prefer code over CLI

import { generate, serialize } from "eco-faker";

const dataset = generate({ seed: 42, scaleFactor: 100 });
const sql = serialize(dataset, "sql"); // or "csv", or just use the JSON directly
Enter fullscreen mode Exit fullscreen mode

Same seed, same output, every time generate() is fully deterministic given (config, referenceNow).

What "relationally consistent" actually means

It's not just that the foreign keys line up. The financials balance (subtotal + tax + shipping === total, to the cent), the timeline makes sense (a shipment's tracking events can't happen before the order that created it), and every line item's productId resolves to a real product that's genuinely reused across orders — not independently invented per line.

Users → Carts → (AbandonedCheckouts | Orders → Shipments → ReturnRequests)
Enter fullscreen mode Exit fullscreen mode

A cart either gets abandoned or converts into an order. An order gets shipped and tracked through a real carrier-style event sequence. Some shipments get returned, with a return reason and a refund that's grounded in the order's real total.

Scenarios

Realistic e-commerce data isn't just "N users, N orders", it's shaped data. eco-faker ships five built-in scenario presets that tune the underlying rates:

my-eco-gen generate --scenario black-friday --users 500
my-eco-gen generate --scenario post-holiday-returns --users 500
my-eco-gen generate --scenario supply-chain-crisis --users 500
Enter fullscreen mode Exit fullscreen mode

black-friday cranks cart volume and abandonment. post-holiday-returns spikes the return rate. supply-chain-crisis introduces real stockouts and delayed shipments. Want a custom mix? Write your own scenario file (YAML or JSON) that inherits from a built-in preset and overrides just the knobs you care about validated against the same schema generate() itself uses, so a typo surfaces immediately instead of silently generating nonsense.

It doesn't stop at "generate"

Once you have a dataset, eco-faker gives you tools to actually use it:

  • my-eco-gen serve: turns any dataset into a live REST API in one command, with pagination, filtering, and even chaos-mode (simulated 500s/429s/latency) for testing how your frontend handles a flaky backend.
  • my-eco-gen lint: checks referential integrity, financial consistency, and temporal ordering before you insert anything into a real database.
  • my-eco-gen fuzz: deliberately mutates the dataset with schema-valid-but-logically-impossible data (inverted prices, oversell quantities) to find bugs your validation layer doesn't catch.
  • MSW, tRPC, GraphQL, Apollo Client, and React Query adapters, so the exact same dataset can back your frontend tests without a second mocking setup.

Try it

npm install -g eco-faker
my-eco-gen generate --scenario black-friday --users 100 --format sql --output ./seed.sql
Enter fullscreen mode Exit fullscreen mode

If you dont have Node then theres a 1-command Docker path that seeds a real Postgres:

docker compose up --build
Enter fullscreen mode Exit fullscreen mode

Top comments (0)