Getting Started With Faker.js: A Developer’s Guide

Testing applications seems easy...until the project gets bigger, and you need complete system testing along with unit and integration testing.…

By Testim,

Testing applications seems easy…until the project gets bigger, and you need complete system testing along with unit and integration testing. But what happens when software testing is primarily data-driven and a chunk of test data doesn’t work? In that case, you need a large amount of mock data to test the system thoroughly.

So, to get a massive set of data, you might need to insert it into the database. What if you could get all the mock data you need to test the software using a simple JavaScript library?

Faker.js is one such popular JavaScript module. It generates large amounts of fake data to ease the process of data-driven testing.

In this post, I’ll help you get started with Faker.js. You’ll learn how to use it in your production system to generate different data types for testing purposes. Here’s a summary of what we’ll cover:

  • Definition of Faker.js
  • Features of Faker.js
  • Installation instructions
  • The types of data in Faker.js
  • How to start generating data
  • Data localization in Faker.js
  • Usage of Helpers
  • The Faker CLI

By the end of the post, you’ll have a solid understanding of what Faker.js is, why you’d want to use it, and how to get started in a practical and easy way.

Faker is an npm module whose sole purpose is to produce massive, well-organized, realistic fake data for testing applications

What Is Faker.js?

Faker is an npm module whose sole purpose is to produce massive, well-organized, realistic fake data for testing applications. The best thing about Faker is that it doesn’t limit you to the server-side. You can use it on the browser side as well.

Faker is very helpful, especially for a tester implementing data-driven testing. In this situation, you’d pass input data in the system as a table of conditions. Then, based on the output received, you’d mark it as pass or fail.

You don’t need any database connection to fetch data. Instead, using a simple script, you can get different types of data. And by combining it with a for loop, you can iterate to get as much data as you want.

What Are the Features of Faker.js?

Let’s find out the types of realistic data that Faker can generate.

  • You can use faker to generate data that is based on time (Present, future, past, etc.)
  • Faker can generate localized realistic data containing region-specific phone numbers or pin codes.
  • Faker can also generate virtual names with complete offline as well as online identification, along with addresses.
  • You can create a virtual transaction, crypto addresses, and account details. Also in the case of a product, you can generate prices, names, and descriptions.
  • Apart from all of the above, you can obviously generate numbers and strings using Faker.

Now that we have learned what Faker can do, let’s discuss how to install Faker.js in your JavaScript project.

How to Install or Add Faker.js in a JavaScript Project

Setting up Faker in your project is a one-step process. If you want to use it in your Node.js project, run a single command:

The command will install the Faker module and automatically add it to the “Package.json” file present in your root project directory as npm dependencies.

Now, include the Faker module in your project using the built-in require function. You can start fetching data using available Faker API methods and helpers.

Instead of Node, maybe you want to use Faker from within your browser. In that case, first, download the latest release file from GitHub and then include it using the script tag.

Types of Data in Faker.js

Before you start using the Faker module, you also need to understand the data you can generate using Faker. Faker provides a wide range of data as the following objects:

names

Syntax

Output example

John Doe

addresses

Syntax

Output example

Sim City

images

Syntax

Output example

https://loremflickr.com/640/480/dogs

animal

Syntax

Output example

Mandarin fish

vehicle

Syntax

Output example

Mercedez Benz

phone

Syntax

Output example

+1 245-272-0145

lorem

Syntax

Output example

“Non architecto nam unde sint…..”

internet

Syntax

Output example

random-domainname.net

git

Syntax

Output example

feat: create products details page

finance

Syntax

Output example

Sets the locale currency and generates output like $3600.

date

Syntax

Output example

Sat Oct 15 2019 06:10:28 GMT-0700 (Pacific Daylight Time)

company

Syntax

Output example

Jones and son

commerce

Syntax

Output example

full sleeve shirt

database

Syntax

Output example

ARCHIVE

datatype

Syntax

Output example

7b16ee12-935e-4agg-8381-b1k457cf0176

helpers

Syntax

Output example

b

Even each of the data categories mentioned above contains several functions to get different sub-data. For instance, git provides the following functions to get fake data:

  • branch
  • commitEntry
  • commitMessage
  • commitSha
  • shortSha

So, if you want to get the fake branch name and commit message, you need to write a little code:

Generate Fake Data Using Faker.js

You’ve learned to install Faker in the Node project, and you understand the different types of data it provides. Now you can start using Faker in your project.

Let’s suppose you want fake vehicle data with details like vehicle name, color, manufacturer, model, type, and fuel. You can use the code below:

What if you want mock data for a user with details like name, job title, and gender? You can use the code shown below. Once you generate the data, you can also format it the way you want, such as in JSON format data.

Using Faker generates random data each time you execute the code. So if you want consistent data that doesn’t change in each execution, you need to set your own seed value.

If you now run the code, you’ll notice that Faker data retrieved after setting seed value gives the same data all the time.

Starting with version 2.0.0, Faker.js added localization support to provide fake data in multiple languages

Get Language-Specific Mock Data Using Faker

Do you want fake data in a language other than English? Faker also has a feature that lets you switch between locales easily.

Starting with version 2.0.0, Faker.js added localization support to provide fake data in multiple languages. Currently, Faker has English as the default locale and supports more than 50 localities. These include Korean, Azerbaijani, French, Farsi/Persian, Turkish, Russian, and Vietnamese.

So, if you want to change your locale, you need to set a new locale value.

Now, if you generate data below this line using Faker, you’ll get fake data only in French.

Here’s another way you can include the locale data for a specific set of locales:

Instead of loading all locale data, the above code loads data for the specified locale only.

Use Helper in Faker

Another important feature that Faker provides is a helper. Currently, if you need user data, you can use faker.name. If you need financial details, you can use faker.finance. And if you need address data, you can use faker.address.

This means you need to call different objects for different data. Here comes a helper that helps you get multiple categories of data at once, easily! Using a helper gives you a large amount of data, combining multiple objects like finance, address, and name in one object.

For example, you want basic user details: name, phone, email, address, company, and website. Instead of using multiple objects for each type of data, use a contextualCard helper that’ll provide all important data easily.

If you run the above code, you’ll get the user card data shown below.

Now, suppose you want more user data on a card. You can use a different helper, createCard. It fetches a large amount of data, including financial, posts, and account history.

Using Faker CLI

You might also need to generate fake data in a context other than when you’re testing an application.  Maybe you’re interacting with some command-line tool and you need a couple of fake names, addresses or dates. What should you do?

In this case, you can use Faker CLI. As the name suggests, this is a CLI (command-line interface tool) to generate fake data. Faker CLI is essentially a wrapper over Faker.js, and you install it using npm as well. Since this is a CLI tool, you’ll want to install it globally, rather than installing it scoped to a single project:

Then, you just have to use it. Here are a few examples.

Generate a Zip Code

Result:

Generate a First Name and Last Name

Generating a first name:

Result:

Gnerating a last name:

Result:

Generate a Phone Number

Results:

Automate Your Testing

To reduce the effort required for repetitive work, it makes sense to use automation. Likewise, if you want to use Faker at scale in your project, you might want to automate the process of testing using fake data.

How can you do that? Testim’s test automation platform is one such solution. Testim also provides insights that you can analyze and scale at any time efficiently.

Testim Automate is a fast and flexible solution with AI-powered end-to-end tests. It also lets you run custom code inside or outside the browser. That way, you can adapt your test to suit nearly any situation.

The good part is you can start Testim for free and use Testim’s custom JS step to pull in data from Faker.js. Even when you want to reuse your custom code across tests, you can save it for reuse, like a function in a programming language.

With Testim, you also get the built-in JavaScript editor that uses Monaco open-source to provide fast, robust syntax checking and code completion.

Use Faker to Mock Massive Data

To sum up, Faker can save you a lot of effort and time. It does this by providing massive fake data using a few lines of code. Instead of writing specific code for mock data and then inserting it into a database, you can use Faker to test your system.

With different types of data, Faker also gives you flexible and organized input. Not to mention, Faker is free and open source.

There’s also Faker-CLI, a command-line that offers a simple wrapper around Faker.js. With the CLI tool, you can quickly and easily generate fake names, addresses, phone numbers and much more. The CLI tool allows you to generate fake test data in a variety of contexts, and it’s particularly useful for automations of all kinds. Also, since it’s a CLI tool installed globally, its usage isn’t restricted to JavaScript: instead, you can use it from any of your favorite shells.

What to Read Next

A Leader’s Guide to Test Data Management (TDM)

The Top 5 Test Data Management Tools

Testim's latest articles, right in your inbox.

From our latest feature releases, to the way it impacts the businesses of our clients, follow the evolution of our product

Blog Subscribe