There are times when you need to test your application using a massive amount of fake data. This way you can leverage your application against a large set of data, all inside a safe test environment. Every programming or scripting language has a way of achieving this; JavaScript is one of them.
 
Probably best-known faker JavaScript framework is Faker.js, I’ll review it first. Other will come later, one by one. This way you can decide what other frameworks can or can’t do, and are they a better choice for you.
 
 

Note: If this tutorial was helpful, need further clarification, something is not working or do you have a request for another Ionic post? Furthermore, if you don't like something about this blog, if something is bugging you, don't like how I'm doing stuff here, again leave me a comment below. I'm here to help you, I expect the same from you. Feel free to comment below, subscribe to my blog, mail me to dragan.gaic@gmail.com, or follow and mention me on twitter (@gajotres). Thanks and have a nice day!

PS. If you want my help, if possible (even if it takes you some time to do that), create a working example I can play with. Use Plunker for AngularJS based questions or jsFiddle for jQuery/jQuery Mobile based questions.


 
Article will cover these frameworks:

1. Faker.js

 
Official Site GitHub
 
 
Faker.js is probably the best known JavaScript faker framework inspired by Ruby’s Gem Faker and Perl’s Data::Faker. Faker.js makes it simple to create any amount of fake data within your browser or on the server using Node.js. You will be able to generate information like names, addresses, phone numbers, internet related data, companies, images, lorem ipsum, dates, random numbers, financial data, etc.
 

Usage

 
Faker.js don’t require additional dependencies, just include it in your application, like this:
 
<script src="http://marak.com/faker.js/js/faker.js"></script>
 

Simple Example

 
Both Node.js and browser versions work out of the box:
 
/* Browser example */

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

/* Node.js example */

var faker = require('./faker');

var randomName = faker.name.findName(); // Rowan Nikolaus
var randomEmail = faker.internet.email(); // Kassandra.Haley@erich.biz
var randomCard = faker.helpers.createCard(); // random contact card containing many properties
 

Localization

 
Localization is also supported, by default it supports 27 different language definition packs. The default language is set to English, so you don’t need to change it if you’re not required to do so.
 
Setting a new locale is simple as this:
 
// sets locale to Germany
faker.locale = "de";
 

API

 
Supported API offers a wide range of available categories (options):
 
Click here if you want to see available API options.

Helpers

 
One of the interesting things about Faker.js are helper methods that let you create bulk fake data using the single API methods. They will make your life easier, instead of working with single fields Faker.js will generate prefilled objects using a single helper method.
 
var contextualCard = faker.helpers.contextualCard();
console.log(contextualCard);

var userCard = faker.helpers.userCard();
console.log(userCard);

var transaction = faker.helpers.createTransaction();
console.log(transaction );

var card = faker.helpers.createCard();
console.log(card);
 

Working example

 
This example uses Ionic Framework and AngularJS though it’s easily readable:
 
Demo
 
Embedded working example:
 
Ionic Framework generate fake data
 

2. Casual.js

 
Official Site GitHub
 
 
Casual.js is a relative newcomer heavily inspired by Faker.js. It supports a very similar set of data, in addition, you’ll find support for Colors, Dates, and Numbers. Even better, if something is missing, you can easily define custom generators.
 
Still I don’t like few things about Casual.js. By default, instead of functions, Casual.js uses object properties to represent randomly generated data. For example, casual.name is not a function but a property with a value that changes each time you access it.
 
Note: This is not the case anymore; a recent change brought generators functions support. Unfortunately, I missed this feature completely; it's described near the documentation end.
 
Another thing is Node.js dependency, browser auto/manual instantiation is not supported.
 

Installation

 
npm install casual
 

Simple Example

 
Only Node.js usage is supported:
 
/* Node.js example */

var casual = require('casual');

var randomName = casual.full_name       // 'Kadin Torphy'
var randomEmail = casual.email          // 'Josue.Hessel@claire.us'
var randomCard = casual.card_data       // { type: 'MasterCard', number: '5307558778577046', exp: '04/88', holder_name: 'Jaron Gibson' }
 

Localization

 
Localization is also supported, from my calculation it supports six different languages. The default locale is en_US, so you don’t need to change it if you’re not required to do so.
 
Setting a new locale is simple as this:
 
// sets locale to Russia
var casual = require('casual').ru_RU;
 

API

 
Supported API offers a wide range of available categories (options):
 
Click here if you want to see available API options.

Helpers

 
Casual.js supports helper methods just like Faker.js though is somewhat different fashion. Where Faker.js provides helpers for predefined objects, Casual.js provides “do it yourself” approach:
 
var custom1 = casual.populate('{{email}} {{first_name}}'); // 'Dallin.Konopelski@yahoo.com Lyla'
console.log(custom1);

var custom2 = casual.populate_one_of(['{{first_name}}', '{{last_name}} {{city}}']);
console.log(custom2);

var custom3 = casual.random_value({ a: 1, b: 3, c: 42 }); // val will be equal 1 or 3 or 42
console.log(userCard);
 
Continue Reading

  • Tracie

    It’s going to be ending of mine day, but before
    finish I am reading this fantastic piece of writing to increase my
    know-how.

  • Thank you for a nice writeup! Much needed because of this regrettable side-effect of OSS, a myriad of libraries doing almost exactly the same thing.