|
| 1 | +# Usage |
| 2 | + |
| 3 | +## Node.js |
| 4 | + |
| 5 | +Using Faker is as easy as importing it from `@faker-js/faker`. |
| 6 | + |
| 7 | +```js |
| 8 | +import { faker } from '@faker-js/faker'; |
| 9 | + |
| 10 | +const randomName = faker.name.fullName(); // Rowan Nikolaus |
| 11 | +const randomEmail = faker.internet.email(); // Kassandra.Haley@erich.biz |
| 12 | +``` |
| 13 | + |
| 14 | +Or if you using CommonJS |
| 15 | + |
| 16 | +```js |
| 17 | +const { faker } = require('@faker-js/faker'); |
| 18 | + |
| 19 | +const randomName = faker.name.fullName(); // Rowan Nikolaus |
| 20 | +const randomEmail = faker.internet.email(); // Kassandra.Haley@erich.biz |
| 21 | +``` |
| 22 | + |
| 23 | +## Browser |
| 24 | + |
| 25 | +```html |
| 26 | +<script type="module"> |
| 27 | + import { faker } from 'https://cdn.skypack.dev/@faker-js/faker'; |
| 28 | +
|
| 29 | + // Caitlyn Kerluke |
| 30 | + const randomName = faker.name.fullName(); |
| 31 | +
|
| 32 | + // Rusty@arne.info |
| 33 | + const randomEmail = faker.internet.email(); |
| 34 | +</script> |
| 35 | +``` |
| 36 | + |
| 37 | +:::note |
| 38 | +Using the browser is great for experimenting 👍. However, due to all of the strings Faker uses to generate fake data, **Faker is a large package**. It's `> 5 MiB` minified. **Please avoid deploying the full Faker in your web app.** |
| 39 | +::: |
| 40 | + |
| 41 | +## CDN/Deno |
| 42 | + |
| 43 | +```js |
| 44 | +import { faker } from 'https://cdn.skypack.dev/@faker-js/faker'; |
| 45 | + |
| 46 | +const randomName = faker.name.findName(); // Willie Bahringer |
| 47 | +const randomEmail = faker.internet.email(); // Tomasa_Ferry14@hotmail.com |
| 48 | +``` |
| 49 | + |
| 50 | +:::note |
| 51 | +It is highly recommended to use version tags when importing libraries in Deno, e.g: `import { faker } from "https://cdn.skypack.dev/@faker-js/faker@v7.4.0"`. Add `?dts` to import with type definitions: `import { faker } from "https://cdn.skypack.dev/@faker-js/faker@v7.4.0?dts"`. |
| 52 | +::: |
| 53 | + |
| 54 | +### Alternative CDN links |
| 55 | + |
| 56 | +**esm:** |
| 57 | + |
| 58 | +- https://esm.sh/@faker-js/faker |
| 59 | +- https://cdn.jsdelivr.net/npm/@faker-js/faker/+esm |
| 60 | + |
| 61 | +**cjs:** |
| 62 | + |
| 63 | +- https://cdn.jsdelivr.net/npm/@faker-js/faker |
| 64 | + |
| 65 | +## TypeScript Support |
| 66 | + |
| 67 | +Faker supports TypeScript out of the box, so you don't have to install any extra packages. |
| 68 | + |
| 69 | +In order to have Faker working properly, you need to check if these `compilerOptions` are set correctly in your `tsconfig` file: |
| 70 | + |
| 71 | +```json |
| 72 | +{ |
| 73 | + "compilerOptions": { |
| 74 | + "esModuleInterop": true, |
| 75 | + "moduleResolution": "Node" |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## Create complex objects |
| 81 | + |
| 82 | +Faker mostly generates values for primitives. |
| 83 | +This is because in the real world, most object schemas simply look very different. |
| 84 | +So, if you want to create an object, you most likely need to write a factory function for it. |
| 85 | + |
| 86 | +For our example, we use TypeScript to strongly type our model. |
| 87 | +The models we will use are described below: |
| 88 | + |
| 89 | +```ts |
| 90 | +import type { SexType } from '@faker-js/faker'; |
| 91 | + |
| 92 | +type SubscriptionTier = 'free' | 'basic' | 'business'; |
| 93 | + |
| 94 | +class User { |
| 95 | + _id: string; |
| 96 | + avatar: string; |
| 97 | + birthday: Date; |
| 98 | + email: string; |
| 99 | + firstName: string; |
| 100 | + lastName: string; |
| 101 | + sex: SexType; |
| 102 | + subscriptionTier: SubscriptionTier; |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +As you can see, your `User` model probably looks completely different from the one you have in your codebase. |
| 107 | +One thing to keep an eye on is the `subscriptionTier` property, as it is not simply a string, but only one of the strings defined in the `SubscriptionTier` type (`'free'` or `'basic'` or `'business'`). |
| 108 | +Also, in a real scenario, your model should not depend on a type of a third party library (`SexType` in this case). |
| 109 | + |
| 110 | +Let's create our first user factory function: |
| 111 | + |
| 112 | +```ts |
| 113 | +import { faker } from '@faker-js/faker'; |
| 114 | + |
| 115 | +function createRandomUser(): User { |
| 116 | + return { |
| 117 | + _id: faker.datatype.uuid(), |
| 118 | + avatar: faker.image.avatar(), |
| 119 | + birthday: faker.date.birthdate(), |
| 120 | + email: faker.internet.email(), |
| 121 | + firstName: faker.name.firstName(), |
| 122 | + lastName: faker.name.lastName(), |
| 123 | + sex: faker.name.sexType(), |
| 124 | + subscriptionTier: faker.helpers.arrayElement(['free', 'basic', 'business']), |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +const user = createRandomUser(); |
| 129 | +``` |
| 130 | + |
| 131 | +At this point, we have a perfectly working function that will work for most purposes. |
| 132 | +But we can take this a step further. |
| 133 | +Currently, all properties are just randomly generated. |
| 134 | +This can lead to some undesirable values being produced. |
| 135 | +For example: The `sex` property having value `'female'` while `firstName` is `'Bob'`. |
| 136 | + |
| 137 | +Let's refactor our current code: |
| 138 | + |
| 139 | +```ts {4-7,13-16} |
| 140 | +import { faker } from '@faker-js/faker'; |
| 141 | + |
| 142 | +function createRandomUser(): User { |
| 143 | + const sex = this.faker.name.sexType(); |
| 144 | + const firstName = faker.name.firstName(sex); |
| 145 | + const lastName = faker.name.lastName(); |
| 146 | + const email = faker.internet.email(firstName, lastName); |
| 147 | + |
| 148 | + return { |
| 149 | + _id: faker.datatype.uuid(), |
| 150 | + avatar: faker.image.avatar(), |
| 151 | + birthday: faker.date.birthdate(), |
| 152 | + email, |
| 153 | + firstName, |
| 154 | + lastName, |
| 155 | + sex, |
| 156 | + subscriptionTier: faker.helpers.arrayElement(['free', 'basic', 'business']), |
| 157 | + }; |
| 158 | +} |
| 159 | + |
| 160 | +const user = createRandomUser(); |
| 161 | +``` |
| 162 | + |
| 163 | +As you can see, we changed the order in which we generate our values. |
| 164 | +First, we generate a `sex` value to use it as input for the generation of `firstName`. |
| 165 | +Then we generate the `lastName`. |
| 166 | +Here, we could also pass in the `sex` value as argument, but in our use-case there are no special cases in where a female last name would differ from a male one. |
| 167 | +By doing this first, we are able to pass both names into the `email` generation function. |
| 168 | +This allows the value to be more reasonable based on the provided arguments. |
| 169 | + |
| 170 | +But we can take this even another step further. |
| 171 | +Opposite to the `_id` property that uses an `uuid` implementation, which is unique by design, the `email` property potentially isn't. |
| 172 | +But, in most use-cases, this would be desirable. |
| 173 | + |
| 174 | +Faker has your back, with another helper method: |
| 175 | + |
| 176 | +```ts {7-9} |
| 177 | +import { faker } from '@faker-js/faker'; |
| 178 | + |
| 179 | +function createRandomUser(): User { |
| 180 | + const sex = this.faker.name.sexType(); |
| 181 | + const firstName = faker.name.firstName(sex); |
| 182 | + const lastName = faker.name.lastName(); |
| 183 | + const email = faker.helpers.unique(faker.internet.email, [ |
| 184 | + firstName, |
| 185 | + lastName, |
| 186 | + ]); |
| 187 | + |
| 188 | + return { |
| 189 | + _id: faker.datatype.uuid(), |
| 190 | + avatar: faker.image.avatar(), |
| 191 | + birthday: faker.date.birthdate(), |
| 192 | + email, |
| 193 | + firstName, |
| 194 | + lastName, |
| 195 | + sex, |
| 196 | + subscriptionTier: faker.helpers.arrayElement(['free', 'basic', 'business']), |
| 197 | + }; |
| 198 | +} |
| 199 | + |
| 200 | +const user = createRandomUser(); |
| 201 | +``` |
| 202 | + |
| 203 | +By wrapping Faker's `email` function with the [`unique`](../api/helpers.md#unique) helper function, we ensure that the return value of `email` is always unique. |
| 204 | + |
| 205 | +Congratulations, you should now be able to create any complex object you desire. Happy faking 🥳. |
0 commit comments