Generating Meaningful Test Data Using Faker
Tags: node, javascript
Published: 2021-01-17 10:49:28

Whether you are building an API or writing tests for features that process massive datasets, meaningful test data is always a necessity. How do we fill this need? Faker is the answer.

<h2>What is Faker?</h2>
Faker is a library that can be used to generates a humongous amount of realistic fake data in Node.js and the browser. It is also available in a variety of other languages such as Python, Perl, Ruby, and C#. This article, however, will focus entirely on the Node.js flavor of Faker.

You can see a live demonstration of faker [here](https://rawgit.com/Marak/faker.js/master/examples/browser/index.html).

<h2>Generating Data using Faker</h2>
Let's consider a use case where we want to store personal information in a CSV file with the following fields:
* First Name
* Last Name
* Address(City, State, Zip Code, Country)
* Phone Number
* Email

And we need **100,000** such records (_meaningful_). Stop for a moment and think how would you have generated this? This is where Faker comes into play.

<h3>Generating CSV Datasets</h3>
Initialize your node project and Install faker:
```
npm i faker
```

Include the dependencies in your project.

```
const faker = require('faker');
const fs = require('fs');
const _ = require('lodash');
```

Define your headers for CSV based on the schema:

```
//define the headers of your csv file.
//define the object literal that would store the functions for each index
//faker generates new data for every call
let csvHeaders = {
    FIRST_NAME: faker.name.firstName(),
    LAST_NAME: faker.name.lastName(),
    STREET_ADDRESS: faker.address.streetAddress(),
    CITY: faker.address.city(),
    STATE: faker.address.state(),
    ZIP_CODE: faker.address.zipCode(),
    COUNTRY: faker.address.country(),
    VOICE_NUMBER: faker.phone.phoneNumber(),
    EMAIL_ADDRESS: faker.internet.email(),
}
```

I am using **streams**, given we are writing input into output sequentially.

```
// open write stream
let stream = fs.createWriteStream("huge-csv.csv");
// write the header line.
stream.write(Object.keys(csvHeaders).toString()+ "\n");
```

Create the 100,000 record CSV file.

```
//write the body
let csvBody = [];
for (let i = 0; i < 1000000; i++) {
    _.forEach(csvHeaders, function(value, key){
        //console.log(value);
        csvBody.push(value);
    })
    //console.log(csvBody.toString(), 'CSV BODY');
    stream.write(csvBody.toString()+ "\n");
    csvBody = [];


}
// close the stream
stream.end(); 
```

<h3>Generating JSON Datasets</h3>
The process of generating the JSON file remains more or less the same with minor tweaks. I will leave that as an exercise. The code is available at [my github repository](https://github.com/Shwetabh1/Generate-Convert-Huge-Dataset-JSON-CSV/tree/master/Generate%20Massive%20Datasets).

<h2> Other Features and API Methods of Faker </h2>

I have only used a subset of the supported API methods in the above example. The faker.js can generate fake data for various other areas, including **commerce, company, date, finance, image, random**, etc.

```
const faker = require('faker');

# Jobs
let jobTitle = faker.name.jobTitle();
console.log(jobTitle);

let jobArea = faker.name.jobArea();
console.log(jobArea);

# dates

let futureDate = faker.date.future();
console.log(futureDate);

let recentDate = faker.date.recent();
console.log(recentDate);

let weekday = faker.date.weekday();
console.log(weekday);

# random values
let number = faker.random.number();
console.log(number);

let uuid = faker.random.uuid();
console.log(uuid);

let word = faker.random.word();
console.log(word);

let words = faker.random.words(6);
console.log(words);

# and so on...
```

You can even use it directly in the browser as well.

```
<script src = "faker.js" type = "text/javascript"></script>
<script>
  var randomName = faker.name.findName(); // Caitlyn Kerluke
  var randomEmail = faker.internet.email(); // Rusty@arne.info
  var randomCard = faker.helpers.createCard(); // random contact card containing many properties
</script>
```

Fake data is extremely useful when building and testing our application and Faker can help us with that. For a complete list of supported APIs, visit this [link](http://marak.github.io/faker.js/). Have you used Faker? How was your experience?