Skip to main content

Faker Data Generation

Faker.js is a test data generation library for JavaScript.

This is used by the tool to generate 'typed' data i.e. non-regex based data.

The values shown in the 'type' drop down menu match the values in the Faker.js API.

Tips Video

Read The Faker API Docs

I recommend using Faker API documentation to understand some of the test data generation functions.

fakerjs.dev/api

Most of the Faker API is implemented.

e.g. I can add a column called "first name" and generate values using faker by using the person.firstName function.

Listed in the API here: https://fakerjs.dev/api/person.html#firstname

Create Test Data Schema as Text then Edit in Grid

I tend to use the "Test Data Text Schema" text input to create an initial schema.

e.g.

first name
a
last name
b
email
e
address
a

And then edit in the grid to pick the faker api functions from the pop up list in the editor.

About Faker

Faker is the test data generation library used by AnyWayData.

It has an extensive API of function calls. e.g. location.buildingNumber to generate a random building number.

In the test data type definition drop down you can find a list of faker function calls in the drop down used on the Test Data Generation data grid.

The items in the drop down are a subset of the function calls, without any parameters.

When you read through the API documentation for faker you will see a lot of options available for the API calls. These are now also available from AnyWayData by typing the data spec into the text area editor.

e.g.

From the drop down menu it is possible to generate a random direction with location.cardinalDirection this will generate North, South etc.

But with the text area it is possible to take advantage of the API for cardinalDirection and pass in a parameter e.g.

Direction
location.cardinalDirection({ abbreviated: true })

The true value for abbreviated parameter will generate short versions of the direction e.g. N, S, E:

Direction
W
N
S
W

More Advanced

Reading through the Faker API documentation we can see that this opens up a lot more possibilities in terms of data generation.

We can now generate dates within a certain range: say between 01/01/2020 and 01/08/2022

Date
date.between({ from: '2020-01-01T00:00:00.000Z', to: '2022-08-01T00:00:00.000Z' })

or

Date
date.between({ from: '2020-01-01', to: '2022-08-01' })

Or if we were working with Finance we could generate banking IBAN numbers for a specific country.

IBAN
finance.iban({ formatted: true, countryCode: 'GB' })
IBANDE
finance.iban({ formatted: false, countryCode: 'DE' })

e.g.

IBANIBANDE
GB93 ELAC 1500 7007 0044 81DE05059809588200970058
GB58 UFYS 0460 3540 0705 65DE58252902443001009070
GB79 WKWS 0040 5932 0004 96DE38438921300010561024
GB72 PRIY 9000 7326 7888 74DE27900409653220700650
GB02 IMHN 0089 8110 0662 92DE74800040080510006033

Numbers

Now we can control the ranges of Number Data Types e.g. generate a number between 32 and 47

Num
number.int({ min: 32, max: 47 })

e.g.

Num
35
41
32
43
41

Helpers and Templates

We have also enabled the helper functions. This allows using Mustache templates directly to create very complex data values.

Mustache Helpers

The Mustache documentation shows some of the capabilities:

Sentence
helpers.mustache('I found {{count}} instances of "{{word}}".', {count: () => `${this.number.int()}`,word: "this word",})

e.g.

Sentence
I found 76980 instances of "this word".
I found 21667 instances of "this word".
I found 23679 instances of "this word".
I found 18850 instances of "this word".
I found 3422 instances of "this word".

The main difference to be aware of when using examples form the faker documentation is that any use of faker in the parameters should be replaced with this.

But now we can create quick complex data strings.

Sentence
helpers.mustache('{{ex}}, A {{adj}} {{noun}}.', {ex: `${this.word.interjection()}`, adj: `${this.word.adjective()}`, noun: `${this.word.noun()}`})
Sentence
tsk tsk, A awkward seed.
ugh, A trustworthy lesson.
aw, A nasty casement.
drat, A unlucky accordion.
yuck, A monthly manhunt.
phooey, A intent bulk.
phooey, A agitated hubcap.
duh, A silent behold.
hmph, A sticky drapes.
er, A suburban tan.

Use the Fake Helper Function to join Faker API functinos

We support the use of the 'fake' function in Faker.

The fake function makes it easy to combine text and Faker calls into a single string.

https://fakerjs.dev/api/helpers.html#fake

e.g. we can use fake in a schema to create an address:

message
helpers.fake('Hi, my name is {{person.firstName}} {{person.lastName}}!')

Copy Schemas For Re-use

And because the schema text field is just a text field, I can copy the schema into Evernote and re-use it later to generate more data:

first name
person.firstName
last name
person.lastName
email
internet.email
address
helpers.fake('{{location.buildingNumber}} {{location.streetAddress}}')