Create fake test data using Node
I'm working on a tool that enables marketers to upload a CSV file full of mobile numbers and send auto-generated surveys through Twilio. Before working on a CSV file uploader, I needed test data. I'm using Faker to create fake data, and CSV stringify to create a CSV file. Once the CSV file is created, I then save it to the filesystem. Finally, to keep the process somewhat organized, I am using Async to implement a waterfall process.
Step 1 - Install Packages
npm i --save moment async csv-stringify faker
Step 2 - Run this Script
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
Modified: 11/11/20
Author: Chris Mendez
URL: http://chrisjmendez.com/2017/08/23/create-fake-emails-for-testing/
Description: Create a CSV file full of fake data.
* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
const fs = require('fs');
const path = require("path");
const moment = require('moment');
const async = require("async");
const stringify = require('csv-stringify');
const faker = require('faker');
faker.locale = "en_US";
var num_of_records = 1000;
async.auto({
//Create fake data
create_fake_data: function (callback) {
let arr = []
// Header
let string = faker.fake(`SMS, Email, First, Last`);
let header = string.split(',');
arr.push(header);
let createEntry = function (sms, email, first, last) {
sms = sms || '{{phone.phoneNumber}}';
email = email || '{{internet.email}}';
last = last || '{{name.lastName}}';
first = first || '{{name.firstName}}';
let string = faker.fake(`${sms}, ${email}, ${last}, ${first}`);
let entry = string.split(',');
return entry;
}
// Create a Custom Entry for Testing
let myData = createEntry(null, 'chris@maildrop.cc', null, null);
arr.push(myData);
// Create Random Entries as Rows
for (var i = 0; i < num_of_records; i++) {
let row = createEntry(null, null, null, null);
arr.push(row);
}
callback(null, arr);
},
//Convert it to CSV
convert_to_csv: ['create_fake_data', function (arr, callback) {
let data = arr.create_fake_data;
let header = data[0];
stringify(data, { columns: header }, function (err, output) {
if (err) console.error(err);
//console.log(output);
callback(null, output);
});
}],
//Save to filesystem
save_to_disk: ['convert_to_csv', function (csv, callback) {
csv = csv.convert_to_csv;
var filePath = `${process.cwd()}/`;
var timestamp = moment().format("YYMMDD-hh_mm_ss");
var fileName = path.join(filePath, timestamp + '.csv');
fs.writeFile(fileName, csv, function (err) {
if (err) return console.log(err);
callback(null);
});
}]
}, function (err, result) {
if(err) console.log("err:", err);
//End Jake Process
process.exit();
});
Resources
- XML to JSON written in Ruby was my inspiration.