Codementor Events

Faker.js - Test your application easily

Published Nov 30, 2019Last updated Dec 04, 2019
Faker.js - Test your application easily

Fake the data easily for testing your application
Generally, we build our application with limited data sets and complete the functionality. But end up finding ways to load bulk data for testing the function thoroughly.
It’s very difficult to get duplicate data for each rows of table and repeat the process to insert multiple rows.
Here comes FAKER.JS
Truly gem of a solution which takes away the pain from us. Faker.js provides you a variety of fake data using it’s node module.
Let’s take an example of how to achieve the same.
Let’s assume I have a user table with following attributes
UserID
EmailID
MobileNumber
Password
Role
DOB
Status
And we want to enter 10 sample data rows for this table.
Step 1: Install faker npm
npm I faker
Step 2: Open Visual Studio Code and create a new .js file called fakertest.js
Step 3: import faker library
var faker = require('faker');
Step 4:
for (counter = 1; counter <= 10; counter++) {
console.log("------------Random Data: " + counter + "--------------");
console.log("UserID is: " + faker.random.uuid());
console.log("User Name is: " + faker.name.findName());
console.log("Email is: " + faker.internet.email());
console.log("Mobile Number is: " + faker.phone.phoneNumber());
console.log("Password is: " + faker.internet.password());
console.log("Role is: " + faker.name.jobType());
console.log("DOB is: " + faker.date.recent());
console.log("Status is: " + faker.random.boolean());
}
Instead of console.log, you can insert the same to database
In the next part we will see how to insert the sample data using faker to DynamoDB.
Refer : https://www.npmjs.com/package/faker for complete details

Discover and read more posts from Kishan
get started
post commentsBe the first to share your opinion
Show more replies