5

Given an enumeration such as:

enum DistributionType {
    Calculated = "Calculated",
    Custom = "Custom",
    None = "None",
}

How do I use Faker.JS to pick a random enum value from it?

const randomValue = faker.???

Reference: https://fakerjs.dev/

Casey Plummer
  • 2,629
  • 23
  • 20

2 Answers2

7

I ended up doing this, as I didn't see any specific helpers for enums.

const randomValue = faker.helpers.arrayElement(Object.values(DistributionType));
Casey Plummer
  • 2,629
  • 23
  • 20
2

For @faker-js/faker > 8.0.0 you can now also use the following helper:

const randomValue = faker.helpers.enumValue(DistributionType);