DEV Community

Manoj Kumar Kengudelu
Manoj Kumar Kengudelu

Posted on

Generate realistic test data files in one command (CSV, JSON, NDJSON)

Every time I needed a CSV of fake users for an import test or a Postman/Newman data-driven run, I ended up writing the same throwaway Faker script. So I turned it into a CLI: csv-faker-generator.

One command, no script

npx csv-faker-generator -c 'id:uuid,name:name,email:email,plan:pick:free|pro|team' -r 3 -o users.csv
Enter fullscreen mode Exit fullscreen mode
id,name,email,plan
5fb9220d-9b0f-4d32-a248-6492457c3890,Miss Arlene Wintheiser,Marlee3@gmail.com,free
a4882fcf-e9e1-4305-a64d-5482c1fc30db,Mr. Ben Buckridge,Anibal63@gmail.com,pro
1bc8c786-010a-458e-b36c-3142ecadc2e8,Erik Farrell,Sabrina.Langosh22@yahoo.com,pro
Enter fullscreen mode Exit fullscreen mode

Columns are header:type. There are 27 types: names, emails, addresses, IBAN/BIC, IPs, UUIDs, numbers, dates, and more.

What's new in 2.2

Reproducible data with --seed. Using the same seed gives you the same file every time, which is useful for snapshot tests and for sharing bug reproductions:

npx csv-faker-generator -c id:uuid,name:name -r 100 --seed 42 -o fixtures.csv
Enter fullscreen mode Exit fullscreen mode

pick values from the CLI. Pick from your own set of values. Quote the argument so your shell doesn't treat | as a pipe:

-c 'status:pick:active|inactive|pending'
Enter fullscreen mode Exit fullscreen mode

JSON and NDJSON output (2.1). The format is inferred from the file extension:

npx csv-faker-generator -c id:uuid,amount:number -r 1000 -o orders.ndjson
Enter fullscreen mode Exit fullscreen mode

Programmatic API

For calculated columns, use it from code. Formulas can reference any other column in the row:

import { generateCSV } from 'csv-faker-generator';

await generateCSV({
  columns: [
    { header: 'qty', type: 'number', min: 1, max: 10 },
    { header: 'price', type: 'number', min: 5, max: 50, decimals: 2 },
    { header: 'total', type: 'formula', formula: (r) => +(r.qty * r.price).toFixed(2) },
  ],
  rows: 500,
  output: 'orders.json',
  format: 'json',
  seed: 7,
});
Enter fullscreen mode Exit fullscreen mode

Built with contributors

Two of the three features in the last two releases came from first-time contributors who picked up good first issue tickets. There's a fresh batch open now, including date ranges, stdout output for piping, and CLI options for alphanumeric. If you want an easy first open-source PR, take a look:

👉 GitHub: https://github.com/manojkken/csv-faker-generator

A ⭐ helps other people find it. Feedback and feature ideas are welcome in the issues.

Top comments (0)